^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/nfs/unlink.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * nfs sillydelete handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/dcache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/sunrpc/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sunrpc/clnt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/nfs_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/fsnotify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "nfs4_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "iostat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "delegation.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "nfstrace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * nfs_free_unlinkdata - release data from a sillydelete operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * @data: pointer to unlink structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) nfs_free_unlinkdata(struct nfs_unlinkdata *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) put_cred(data->cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) kfree(data->args.name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * nfs_async_unlink_done - Sillydelete post-processing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @task: rpc_task of the sillydelete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @calldata: pointer to nfs_unlinkdata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Do the directory attribute update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct nfs_unlinkdata *data = calldata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct inode *dir = d_inode(data->dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) trace_nfs_sillyrename_unlink(data, task->tk_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!NFS_PROTO(dir)->unlink_done(task, dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) rpc_restart_call_prepare(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * nfs_async_unlink_release - Release the sillydelete data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * @calldata: struct nfs_unlinkdata to release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * rpc_task would be freed too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static void nfs_async_unlink_release(void *calldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct nfs_unlinkdata *data = calldata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct dentry *dentry = data->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct super_block *sb = dentry->d_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) up_read_non_owner(&NFS_I(d_inode(dentry->d_parent))->rmdir_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) d_lookup_done(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) nfs_free_unlinkdata(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) nfs_sb_deactive(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static void nfs_unlink_prepare(struct rpc_task *task, void *calldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct nfs_unlinkdata *data = calldata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct inode *dir = d_inode(data->dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) NFS_PROTO(dir)->unlink_rpc_prepare(task, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static const struct rpc_call_ops nfs_unlink_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .rpc_call_done = nfs_async_unlink_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .rpc_release = nfs_async_unlink_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .rpc_call_prepare = nfs_unlink_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static void nfs_do_call_unlink(struct inode *inode, struct nfs_unlinkdata *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct rpc_message msg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .rpc_argp = &data->args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .rpc_resp = &data->res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .rpc_cred = data->cred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct rpc_task_setup task_setup_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) .rpc_message = &msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .callback_ops = &nfs_unlink_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .callback_data = data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .workqueue = nfsiod_workqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct rpc_task *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct inode *dir = d_inode(data->dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) nfs_sb_active(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) data->args.fh = NFS_FH(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) nfs_fattr_init(data->res.dir_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) NFS_PROTO(dir)->unlink_setup(&msg, data->dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) task_setup_data.rpc_client = NFS_CLIENT(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) task = rpc_run_task(&task_setup_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (!IS_ERR(task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) rpc_put_task_async(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int nfs_call_unlink(struct dentry *dentry, struct inode *inode, struct nfs_unlinkdata *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct inode *dir = d_inode(dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct dentry *alias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) down_read_non_owner(&NFS_I(dir)->rmdir_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) alias = d_alloc_parallel(dentry->d_parent, &data->args.name, &data->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (IS_ERR(alias)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) up_read_non_owner(&NFS_I(dir)->rmdir_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (!d_in_lookup(alias)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) void *devname_garbage = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * Hey, we raced with lookup... See if we need to transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * the sillyrename information to the aliased dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) spin_lock(&alias->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (d_really_is_positive(alias) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) devname_garbage = alias->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) alias->d_fsdata = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) alias->d_flags |= DCACHE_NFSFS_RENAMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) spin_unlock(&alias->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) dput(alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) up_read_non_owner(&NFS_I(dir)->rmdir_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * If we'd displaced old cached devname, free it. At that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * point dentry is definitely not a root, so we won't need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * that anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) kfree(devname_garbage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) data->dentry = alias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) nfs_do_call_unlink(inode, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * nfs_async_unlink - asynchronous unlinking of a file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * @dentry: parent directory of dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * @name: name of dentry to unlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) nfs_async_unlink(struct dentry *dentry, const struct qstr *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct nfs_unlinkdata *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) void *devname_garbage = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) data = kzalloc(sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) data->args.name.name = kstrdup(name->name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (!data->args.name.name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) data->args.name.len = name->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) data->cred = get_current_cred();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) data->res.dir_attr = &data->dir_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) init_waitqueue_head(&data->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) status = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) spin_lock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) dentry->d_flags |= DCACHE_NFSFS_RENAMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) devname_garbage = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) dentry->d_fsdata = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) spin_unlock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * If we'd displaced old cached devname, free it. At that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * point dentry is definitely not a root, so we won't need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * that anymore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) kfree(devname_garbage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) spin_unlock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) put_cred(data->cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) kfree(data->args.name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * nfs_complete_unlink - Initialize completion of the sillydelete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * @dentry: dentry to delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * @inode: inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * Since we're most likely to be called by dentry_iput(), we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * only use the dentry to find the sillydelete. We then copy the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * into the qstr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct nfs_unlinkdata *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) spin_lock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) data = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) dentry->d_fsdata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) spin_unlock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (NFS_STALE(inode) || !nfs_call_unlink(dentry, inode, data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) nfs_free_unlinkdata(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /* Cancel a queued async unlink. Called when a sillyrename run fails. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) nfs_cancel_async_unlink(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) spin_lock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct nfs_unlinkdata *data = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) dentry->d_fsdata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) spin_unlock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) nfs_free_unlinkdata(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) spin_unlock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * nfs_async_rename_done - Sillyrename post-processing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * @task: rpc_task of the sillyrename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * @calldata: nfs_renamedata for the sillyrename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * Do the directory attribute updates and the d_move
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static void nfs_async_rename_done(struct rpc_task *task, void *calldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct nfs_renamedata *data = calldata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct inode *old_dir = data->old_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct inode *new_dir = data->new_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct dentry *old_dentry = data->old_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) trace_nfs_sillyrename_rename(old_dir, old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) new_dir, data->new_dentry, task->tk_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (!NFS_PROTO(old_dir)->rename_done(task, old_dir, new_dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) rpc_restart_call_prepare(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (data->complete)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) data->complete(task, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * nfs_async_rename_release - Release the sillyrename data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * @calldata: the struct nfs_renamedata to be released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static void nfs_async_rename_release(void *calldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct nfs_renamedata *data = calldata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct super_block *sb = data->old_dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (d_really_is_positive(data->old_dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) nfs_mark_for_revalidate(d_inode(data->old_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) /* The result of the rename is unknown. Play it safe by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * forcing a new lookup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (data->cancelled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) spin_lock(&data->old_dir->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) nfs_force_lookup_revalidate(data->old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) spin_unlock(&data->old_dir->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (data->new_dir != data->old_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) spin_lock(&data->new_dir->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) nfs_force_lookup_revalidate(data->new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) spin_unlock(&data->new_dir->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) dput(data->old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) dput(data->new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) iput(data->old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) iput(data->new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) nfs_sb_deactive(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) put_cred(data->cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static void nfs_rename_prepare(struct rpc_task *task, void *calldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct nfs_renamedata *data = calldata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) NFS_PROTO(data->old_dir)->rename_rpc_prepare(task, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static const struct rpc_call_ops nfs_rename_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) .rpc_call_done = nfs_async_rename_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) .rpc_release = nfs_async_rename_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) .rpc_call_prepare = nfs_rename_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * nfs_async_rename - perform an asynchronous rename operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * @old_dir: directory that currently holds the dentry to be renamed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * @new_dir: target directory for the rename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * @old_dentry: original dentry to be renamed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * @new_dentry: dentry to which the old_dentry should be renamed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * @complete: Function to run on successful completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * It's expected that valid references to the dentries and inodes are held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct rpc_task *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) nfs_async_rename(struct inode *old_dir, struct inode *new_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct dentry *old_dentry, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) void (*complete)(struct rpc_task *, struct nfs_renamedata *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct nfs_renamedata *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) struct rpc_message msg = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) struct rpc_task_setup task_setup_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) .rpc_message = &msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) .callback_ops = &nfs_rename_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) .workqueue = nfsiod_workqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) .rpc_client = NFS_CLIENT(old_dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) data = kzalloc(sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) task_setup_data.callback_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) data->cred = get_current_cred();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) msg.rpc_argp = &data->args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) msg.rpc_resp = &data->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) msg.rpc_cred = data->cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /* set up nfs_renamedata */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) data->old_dir = old_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) ihold(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) data->new_dir = new_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) ihold(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) data->old_dentry = dget(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) data->new_dentry = dget(new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) nfs_fattr_init(&data->old_fattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) nfs_fattr_init(&data->new_fattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) data->complete = complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /* set up nfs_renameargs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) data->args.old_dir = NFS_FH(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) data->args.old_name = &old_dentry->d_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) data->args.new_dir = NFS_FH(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) data->args.new_name = &new_dentry->d_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /* set up nfs_renameres */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) data->res.old_fattr = &data->old_fattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) data->res.new_fattr = &data->new_fattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) nfs_sb_active(old_dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) NFS_PROTO(data->old_dir)->rename_setup(&msg, old_dentry, new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return rpc_run_task(&task_setup_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * Perform tasks needed when a sillyrename is done such as cancelling the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * queued async unlink if it failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) nfs_complete_sillyrename(struct rpc_task *task, struct nfs_renamedata *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct dentry *dentry = data->old_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (task->tk_status != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) nfs_cancel_async_unlink(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) #define SILLYNAME_PREFIX ".nfs"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) #define SILLYNAME_PREFIX_LEN ((unsigned)sizeof(SILLYNAME_PREFIX) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) #define SILLYNAME_FILEID_LEN ((unsigned)sizeof(u64) << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) #define SILLYNAME_COUNTER_LEN ((unsigned)sizeof(unsigned int) << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) #define SILLYNAME_LEN (SILLYNAME_PREFIX_LEN + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) SILLYNAME_FILEID_LEN + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) SILLYNAME_COUNTER_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * nfs_sillyrename - Perform a silly-rename of a dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * @dir: inode of directory that contains dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * @dentry: dentry to be sillyrenamed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * NFSv2/3 is stateless and the server doesn't know when the client is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * holding a file open. To prevent application problems when a file is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * unlinked while it's still open, the client performs a "silly-rename".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * That is, it renames the file to a hidden file in the same directory,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * and only performs the unlink once the last reference to it is put.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * The final cleanup is done during dentry_iput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * (Note: NFSv4 is stateful, and has opens, so in theory an NFSv4 server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * could take responsibility for keeping open files referenced. The server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * would also need to ensure that opened-but-deleted files were kept over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * reboots. However, we may not assume a server does so. (RFC 5661
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * does provide an OPEN4_RESULT_PRESERVE_UNLINKED flag that a server can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * use to advertise that it does this; some day we may take advantage of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * it.))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) nfs_sillyrename(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static unsigned int sillycounter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) unsigned char silly[SILLYNAME_LEN + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) unsigned long long fileid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) struct dentry *sdentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) struct rpc_task *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) int error = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) dfprintk(VFS, "NFS: silly-rename(%pd2, ct=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) dentry, d_count(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * We don't allow a dentry to be silly-renamed twice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) fileid = NFS_FILEID(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) sdentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) int slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) dput(sdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) sillycounter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) slen = scnprintf(silly, sizeof(silly),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) SILLYNAME_PREFIX "%0*llx%0*x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) SILLYNAME_FILEID_LEN, fileid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) SILLYNAME_COUNTER_LEN, sillycounter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) dfprintk(VFS, "NFS: trying to rename %pd to %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) dentry, silly);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) sdentry = lookup_one_len(silly, dentry->d_parent, slen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * N.B. Better to return EBUSY here ... it could be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * dangerous to delete the file while it's in use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (IS_ERR(sdentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) } while (d_inode(sdentry) != NULL); /* need negative lookup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) ihold(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) /* queue unlink first. Can't do this from rpc_release as it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * has to allocate memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) error = nfs_async_unlink(dentry, &sdentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) /* run the rename task, undo unlink if it fails */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) task = nfs_async_rename(dir, dir, dentry, sdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) nfs_complete_sillyrename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (IS_ERR(task)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) error = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) nfs_cancel_async_unlink(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) /* wait for the RPC task to complete, unless a SIGKILL intervenes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) error = rpc_wait_for_completion_task(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (error == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) error = task->tk_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) switch (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) /* The rename succeeded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) | NFS_INO_INVALID_CTIME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) | NFS_INO_REVAL_FORCED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) d_move(dentry, sdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) case -ERESTARTSYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) /* The result of the rename is unknown. Play it safe by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * forcing a new lookup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) d_drop(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) d_drop(sdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) rpc_put_task(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) out_dput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) dput(sdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }