unlink.c 14.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  linux/fs/nfs/unlink.c
 *
 * nfs sillydelete handling
 *
 */

#include <linux/slab.h>
#include <linux/string.h>
#include <linux/dcache.h>
#include <linux/sunrpc/sched.h>
#include <linux/sunrpc/clnt.h>
#include <linux/nfs_fs.h>
15 16
#include <linux/sched.h>
#include <linux/wait.h>
17
#include <linux/namei.h>
18
#include <linux/fsnotify.h>
L
Linus Torvalds 已提交
19

20
#include "internal.h"
21
#include "nfs4_fs.h"
22 23
#include "iostat.h"
#include "delegation.h"
24

25 26
#include "nfstrace.h"

L
Linus Torvalds 已提交
27
/**
28
 * nfs_free_unlinkdata - release data from a sillydelete operation.
L
Linus Torvalds 已提交
29 30 31
 * @data: pointer to unlink structure.
 */
static void
32
nfs_free_unlinkdata(struct nfs_unlinkdata *data)
L
Linus Torvalds 已提交
33
{
34
	put_cred(data->cred);
35 36
	kfree(data->args.name.name);
	kfree(data);
L
Linus Torvalds 已提交
37 38 39 40 41
}

/**
 * nfs_async_unlink_done - Sillydelete post-processing
 * @task: rpc_task of the sillydelete
42
 * @calldata: pointer to nfs_unlinkdata
L
Linus Torvalds 已提交
43 44 45
 *
 * Do the directory attribute update.
 */
46
static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
L
Linus Torvalds 已提交
47
{
48
	struct nfs_unlinkdata *data = calldata;
A
Al Viro 已提交
49
	struct inode *dir = d_inode(data->dentry->d_parent);
50

51
	trace_nfs_sillyrename_unlink(data, task->tk_status);
52
	if (!NFS_PROTO(dir)->unlink_done(task, dir))
53
		rpc_restart_call_prepare(task);
L
Linus Torvalds 已提交
54 55 56 57
}

/**
 * nfs_async_unlink_release - Release the sillydelete data.
58
 * @calldata: struct nfs_unlinkdata to release
L
Linus Torvalds 已提交
59 60 61 62
 *
 * We need to call nfs_put_unlinkdata as a 'tk_release' task since the
 * rpc_task would be freed too.
 */
63
static void nfs_async_unlink_release(void *calldata)
L
Linus Torvalds 已提交
64
{
65
	struct nfs_unlinkdata	*data = calldata;
A
Al Viro 已提交
66 67
	struct dentry *dentry = data->dentry;
	struct super_block *sb = dentry->d_sb;
T
Trond Myklebust 已提交
68

A
Al Viro 已提交
69 70
	up_read_non_owner(&NFS_I(d_inode(dentry->d_parent))->rmdir_sem);
	d_lookup_done(dentry);
71
	nfs_free_unlinkdata(data);
A
Al Viro 已提交
72
	dput(dentry);
73
	nfs_sb_deactive(sb);
L
Linus Torvalds 已提交
74 75
}

76
static void nfs_unlink_prepare(struct rpc_task *task, void *calldata)
77 78
{
	struct nfs_unlinkdata *data = calldata;
A
Al Viro 已提交
79 80
	struct inode *dir = d_inode(data->dentry->d_parent);
	NFS_PROTO(dir)->unlink_rpc_prepare(task, data);
81 82
}

83 84 85
static const struct rpc_call_ops nfs_unlink_ops = {
	.rpc_call_done = nfs_async_unlink_done,
	.rpc_release = nfs_async_unlink_release,
86
	.rpc_call_prepare = nfs_unlink_prepare,
87 88
};

89
static void nfs_do_call_unlink(struct inode *inode, struct nfs_unlinkdata *data)
90
{
91 92 93 94 95
	struct rpc_message msg = {
		.rpc_argp = &data->args,
		.rpc_resp = &data->res,
		.rpc_cred = data->cred,
	};
T
Trond Myklebust 已提交
96
	struct rpc_task_setup task_setup_data = {
97
		.rpc_message = &msg,
T
Trond Myklebust 已提交
98 99
		.callback_ops = &nfs_unlink_ops,
		.callback_data = data,
100
		.workqueue = nfsiod_workqueue,
101
		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
T
Trond Myklebust 已提交
102
	};
103
	struct rpc_task *task;
A
Al Viro 已提交
104 105 106 107 108
	struct inode *dir = d_inode(data->dentry->d_parent);
	nfs_sb_active(dir->i_sb);
	data->args.fh = NFS_FH(dir);
	nfs_fattr_init(data->res.dir_attr);

109
	NFS_PROTO(dir)->unlink_setup(&msg, data->dentry, inode);
A
Al Viro 已提交
110 111 112 113 114 115 116

	task_setup_data.rpc_client = NFS_CLIENT(dir);
	task = rpc_run_task(&task_setup_data);
	if (!IS_ERR(task))
		rpc_put_task_async(task);
}

117
static int nfs_call_unlink(struct dentry *dentry, struct inode *inode, struct nfs_unlinkdata *data)
A
Al Viro 已提交
118 119
{
	struct inode *dir = d_inode(dentry->d_parent);
T
Trond Myklebust 已提交
120 121
	struct dentry *alias;

A
Al Viro 已提交
122 123 124 125 126 127 128
	down_read_non_owner(&NFS_I(dir)->rmdir_sem);
	alias = d_alloc_parallel(dentry->d_parent, &data->args.name, &data->wq);
	if (IS_ERR(alias)) {
		up_read_non_owner(&NFS_I(dir)->rmdir_sem);
		return 0;
	}
	if (!d_in_lookup(alias)) {
129
		int ret;
130
		void *devname_garbage = NULL;
131

T
Trond Myklebust 已提交
132 133 134 135 136
		/*
		 * Hey, we raced with lookup... See if we need to transfer
		 * the sillyrename information to the aliased dentry.
		 */
		spin_lock(&alias->d_lock);
A
Al Viro 已提交
137
		if (d_really_is_positive(alias) &&
138
		    !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
139
			devname_garbage = alias->d_fsdata;
T
Trond Myklebust 已提交
140
			alias->d_fsdata = data;
T
Trond Myklebust 已提交
141
			alias->d_flags |= DCACHE_NFSFS_RENAMED;
T
Trond Myklebust 已提交
142
			ret = 1;
143 144
		} else
			ret = 0;
T
Trond Myklebust 已提交
145 146
		spin_unlock(&alias->d_lock);
		dput(alias);
A
Al Viro 已提交
147
		up_read_non_owner(&NFS_I(dir)->rmdir_sem);
148 149 150 151 152
		/*
		 * If we'd displaced old cached devname, free it.  At that
		 * point dentry is definitely not a root, so we won't need
		 * that anymore.
		 */
153
		kfree(devname_garbage);
T
Trond Myklebust 已提交
154 155
		return ret;
	}
A
Al Viro 已提交
156
	data->dentry = alias;
157
	nfs_do_call_unlink(inode, data);
T
Trond Myklebust 已提交
158 159 160
	return 1;
}

L
Linus Torvalds 已提交
161 162
/**
 * nfs_async_unlink - asynchronous unlinking of a file
163 164
 * @dentry: parent directory of dentry
 * @name: name of dentry to unlink
L
Linus Torvalds 已提交
165
 */
166
static int
A
Al Viro 已提交
167
nfs_async_unlink(struct dentry *dentry, const struct qstr *name)
L
Linus Torvalds 已提交
168
{
169 170
	struct nfs_unlinkdata *data;
	int status = -ENOMEM;
171
	void *devname_garbage = NULL;
L
Linus Torvalds 已提交
172

173
	data = kzalloc(sizeof(*data), GFP_KERNEL);
174
	if (data == NULL)
L
Linus Torvalds 已提交
175
		goto out;
A
Al Viro 已提交
176 177 178 179
	data->args.name.name = kstrdup(name->name, GFP_KERNEL);
	if (!data->args.name.name)
		goto out_free;
	data->args.name.len = name->len;
L
Linus Torvalds 已提交
180

181
	data->cred = get_current_cred();
182
	data->res.dir_attr = &data->dir_attr;
A
Al Viro 已提交
183
	init_waitqueue_head(&data->wq);
L
Linus Torvalds 已提交
184

185
	status = -EBUSY;
L
Linus Torvalds 已提交
186
	spin_lock(&dentry->d_lock);
187 188
	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
		goto out_unlock;
L
Linus Torvalds 已提交
189
	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
190
	devname_garbage = dentry->d_fsdata;
191
	dentry->d_fsdata = data;
L
Linus Torvalds 已提交
192
	spin_unlock(&dentry->d_lock);
193 194 195 196 197
	/*
	 * If we'd displaced old cached devname, free it.  At that
	 * point dentry is definitely not a root, so we won't need
	 * that anymore.
	 */
198
	kfree(devname_garbage);
199 200 201
	return 0;
out_unlock:
	spin_unlock(&dentry->d_lock);
202
	put_cred(data->cred);
A
Al Viro 已提交
203
	kfree(data->args.name.name);
L
Linus Torvalds 已提交
204 205
out_free:
	kfree(data);
206
out:
L
Linus Torvalds 已提交
207 208 209 210 211 212
	return status;
}

/**
 * nfs_complete_unlink - Initialize completion of the sillydelete
 * @dentry: dentry to delete
213
 * @inode: inode
L
Linus Torvalds 已提交
214 215 216 217 218 219
 *
 * Since we're most likely to be called by dentry_iput(), we
 * only use the dentry to find the sillydelete. We then copy the name
 * into the qstr.
 */
void
220
nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
L
Linus Torvalds 已提交
221
{
A
Al Viro 已提交
222
	struct nfs_unlinkdata	*data;
L
Linus Torvalds 已提交
223 224

	spin_lock(&dentry->d_lock);
A
Al Viro 已提交
225 226 227
	dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
	data = dentry->d_fsdata;
	dentry->d_fsdata = NULL;
L
Linus Torvalds 已提交
228
	spin_unlock(&dentry->d_lock);
229

230
	if (NFS_STALE(inode) || !nfs_call_unlink(dentry, inode, data))
231
		nfs_free_unlinkdata(data);
L
Linus Torvalds 已提交
232
}
233

234 235 236 237 238 239 240 241 242
/* Cancel a queued async unlink. Called when a sillyrename run fails. */
static void
nfs_cancel_async_unlink(struct dentry *dentry)
{
	spin_lock(&dentry->d_lock);
	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
		struct nfs_unlinkdata *data = dentry->d_fsdata;

		dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
243
		dentry->d_fsdata = NULL;
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
		spin_unlock(&dentry->d_lock);
		nfs_free_unlinkdata(data);
		return;
	}
	spin_unlock(&dentry->d_lock);
}

/**
 * nfs_async_rename_done - Sillyrename post-processing
 * @task: rpc_task of the sillyrename
 * @calldata: nfs_renamedata for the sillyrename
 *
 * Do the directory attribute updates and the d_move
 */
static void nfs_async_rename_done(struct rpc_task *task, void *calldata)
{
	struct nfs_renamedata *data = calldata;
	struct inode *old_dir = data->old_dir;
	struct inode *new_dir = data->new_dir;
263
	struct dentry *old_dentry = data->old_dentry;
264

265 266
	trace_nfs_sillyrename_rename(old_dir, old_dentry,
			new_dir, data->new_dentry, task->tk_status);
267
	if (!NFS_PROTO(old_dir)->rename_done(task, old_dir, new_dir)) {
268
		rpc_restart_call_prepare(task);
269 270 271
		return;
	}

272 273
	if (data->complete)
		data->complete(task, data);
274 275 276 277 278 279 280 281 282 283 284
}

/**
 * nfs_async_rename_release - Release the sillyrename data.
 * @calldata: the struct nfs_renamedata to be released
 */
static void nfs_async_rename_release(void *calldata)
{
	struct nfs_renamedata	*data = calldata;
	struct super_block *sb = data->old_dir->i_sb;

285 286
	if (d_really_is_positive(data->old_dentry))
		nfs_mark_for_revalidate(d_inode(data->old_dentry));
287

288 289 290 291 292 293 294 295 296 297 298 299 300
	/* The result of the rename is unknown. Play it safe by
	 * forcing a new lookup */
	if (data->cancelled) {
		spin_lock(&data->old_dir->i_lock);
		nfs_force_lookup_revalidate(data->old_dir);
		spin_unlock(&data->old_dir->i_lock);
		if (data->new_dir != data->old_dir) {
			spin_lock(&data->new_dir->i_lock);
			nfs_force_lookup_revalidate(data->new_dir);
			spin_unlock(&data->new_dir->i_lock);
		}
	}

301 302 303 304 305
	dput(data->old_dentry);
	dput(data->new_dentry);
	iput(data->old_dir);
	iput(data->new_dir);
	nfs_sb_deactive(sb);
306
	put_cred(data->cred);
307 308 309 310 311 312
	kfree(data);
}

static void nfs_rename_prepare(struct rpc_task *task, void *calldata)
{
	struct nfs_renamedata *data = calldata;
313
	NFS_PROTO(data->old_dir)->rename_rpc_prepare(task, data);
314 315 316 317 318 319 320 321 322 323 324 325 326 327
}

static const struct rpc_call_ops nfs_rename_ops = {
	.rpc_call_done = nfs_async_rename_done,
	.rpc_release = nfs_async_rename_release,
	.rpc_call_prepare = nfs_rename_prepare,
};

/**
 * nfs_async_rename - perform an asynchronous rename operation
 * @old_dir: directory that currently holds the dentry to be renamed
 * @new_dir: target directory for the rename
 * @old_dentry: original dentry to be renamed
 * @new_dentry: dentry to which the old_dentry should be renamed
328
 * @complete: Function to run on successful completion
329 330 331
 *
 * It's expected that valid references to the dentries and inodes are held
 */
332
struct rpc_task *
333
nfs_async_rename(struct inode *old_dir, struct inode *new_dir,
334 335
		 struct dentry *old_dentry, struct dentry *new_dentry,
		 void (*complete)(struct rpc_task *, struct nfs_renamedata *))
336 337 338 339 340 341 342 343
{
	struct nfs_renamedata *data;
	struct rpc_message msg = { };
	struct rpc_task_setup task_setup_data = {
		.rpc_message = &msg,
		.callback_ops = &nfs_rename_ops,
		.workqueue = nfsiod_workqueue,
		.rpc_client = NFS_CLIENT(old_dir),
344
		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
345 346
	};

347
	data = kzalloc(sizeof(*data), GFP_KERNEL);
348 349
	if (data == NULL)
		return ERR_PTR(-ENOMEM);
350
	task_setup_data.callback_data = data;
351

352
	data->cred = get_current_cred();
353 354 355 356 357 358 359

	msg.rpc_argp = &data->args;
	msg.rpc_resp = &data->res;
	msg.rpc_cred = data->cred;

	/* set up nfs_renamedata */
	data->old_dir = old_dir;
360
	ihold(old_dir);
361
	data->new_dir = new_dir;
362
	ihold(new_dir);
363 364 365 366
	data->old_dentry = dget(old_dentry);
	data->new_dentry = dget(new_dentry);
	nfs_fattr_init(&data->old_fattr);
	nfs_fattr_init(&data->new_fattr);
367
	data->complete = complete;
368 369 370 371 372 373 374 375 376 377 378 379 380

	/* set up nfs_renameargs */
	data->args.old_dir = NFS_FH(old_dir);
	data->args.old_name = &old_dentry->d_name;
	data->args.new_dir = NFS_FH(new_dir);
	data->args.new_name = &new_dentry->d_name;

	/* set up nfs_renameres */
	data->res.old_fattr = &data->old_fattr;
	data->res.new_fattr = &data->new_fattr;

	nfs_sb_active(old_dir->i_sb);

381
	NFS_PROTO(data->old_dir)->rename_setup(&msg, old_dentry, new_dentry);
382

383
	return rpc_run_task(&task_setup_data);
384 385
}

386 387 388 389 390 391 392
/*
 * Perform tasks needed when a sillyrename is done such as cancelling the
 * queued async unlink if it failed.
 */
static void
nfs_complete_sillyrename(struct rpc_task *task, struct nfs_renamedata *data)
{
393 394 395 396 397 398
	struct dentry *dentry = data->old_dentry;

	if (task->tk_status != 0) {
		nfs_cancel_async_unlink(dentry);
		return;
	}
399 400
}

401 402 403 404 405 406 407 408
#define SILLYNAME_PREFIX ".nfs"
#define SILLYNAME_PREFIX_LEN ((unsigned)sizeof(SILLYNAME_PREFIX) - 1)
#define SILLYNAME_FILEID_LEN ((unsigned)sizeof(u64) << 1)
#define SILLYNAME_COUNTER_LEN ((unsigned)sizeof(unsigned int) << 1)
#define SILLYNAME_LEN (SILLYNAME_PREFIX_LEN + \
		SILLYNAME_FILEID_LEN + \
		SILLYNAME_COUNTER_LEN)

409 410 411 412 413 414 415 416 417 418 419 420
/**
 * nfs_sillyrename - Perform a silly-rename of a dentry
 * @dir: inode of directory that contains dentry
 * @dentry: dentry to be sillyrenamed
 *
 * NFSv2/3 is stateless and the server doesn't know when the client is
 * holding a file open. To prevent application problems when a file is
 * unlinked while it's still open, the client performs a "silly-rename".
 * That is, it renames the file to a hidden file in the same directory,
 * and only performs the unlink once the last reference to it is put.
 *
 * The final cleanup is done during dentry_iput.
421 422 423 424 425 426 427 428
 *
 * (Note: NFSv4 is stateful, and has opens, so in theory an NFSv4 server
 * could take responsibility for keeping open files referenced.  The server
 * would also need to ensure that opened-but-deleted files were kept over
 * reboots.  However, we may not assume a server does so.  (RFC 5661
 * does provide an OPEN4_RESULT_PRESERVE_UNLINKED flag that a server can
 * use to advertise that it does this; some day we may take advantage of
 * it.))
429 430 431 432 433
 */
int
nfs_sillyrename(struct inode *dir, struct dentry *dentry)
{
	static unsigned int sillycounter;
434 435
	unsigned char silly[SILLYNAME_LEN + 1];
	unsigned long long fileid;
436
	struct dentry *sdentry;
T
Trond Myklebust 已提交
437
	struct inode *inode = d_inode(dentry);
438
	struct rpc_task *task;
439
	int            error = -EBUSY;
440

441 442
	dfprintk(VFS, "NFS: silly-rename(%pd2, ct=%d)\n",
		dentry, d_count(dentry));
443 444 445 446 447 448 449 450
	nfs_inc_stats(dir, NFSIOS_SILLYRENAME);

	/*
	 * We don't allow a dentry to be silly-renamed twice.
	 */
	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
		goto out;

451
	fileid = NFS_FILEID(d_inode(dentry));
452 453 454

	sdentry = NULL;
	do {
455
		int slen;
456 457
		dput(sdentry);
		sillycounter++;
458 459 460 461
		slen = scnprintf(silly, sizeof(silly),
				SILLYNAME_PREFIX "%0*llx%0*x",
				SILLYNAME_FILEID_LEN, fileid,
				SILLYNAME_COUNTER_LEN, sillycounter);
462

463 464
		dfprintk(VFS, "NFS: trying to rename %pd to %s\n",
				dentry, silly);
465 466 467 468 469 470 471 472

		sdentry = lookup_one_len(silly, dentry->d_parent, slen);
		/*
		 * N.B. Better to return EBUSY here ... it could be
		 * dangerous to delete the file while it's in use.
		 */
		if (IS_ERR(sdentry))
			goto out;
473
	} while (d_inode(sdentry) != NULL); /* need negative lookup */
474

T
Trond Myklebust 已提交
475 476
	ihold(inode);

477 478 479
	/* queue unlink first. Can't do this from rpc_release as it
	 * has to allocate memory
	 */
A
Al Viro 已提交
480
	error = nfs_async_unlink(dentry, &sdentry->d_name);
481 482 483 484
	if (error)
		goto out_dput;

	/* run the rename task, undo unlink if it fails */
485 486
	task = nfs_async_rename(dir, dir, dentry, sdentry,
					nfs_complete_sillyrename);
487 488 489 490
	if (IS_ERR(task)) {
		error = -EBUSY;
		nfs_cancel_async_unlink(dentry);
		goto out_dput;
491
	}
492 493 494 495 496

	/* wait for the RPC task to complete, unless a SIGKILL intervenes */
	error = rpc_wait_for_completion_task(task);
	if (error == 0)
		error = task->tk_status;
497 498 499 500
	switch (error) {
	case 0:
		/* The rename succeeded */
		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
T
Trond Myklebust 已提交
501 502 503 504 505 506
		spin_lock(&inode->i_lock);
		NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
			| NFS_INO_INVALID_CTIME
			| NFS_INO_REVAL_FORCED;
		spin_unlock(&inode->i_lock);
507 508 509 510 511 512 513 514
		d_move(dentry, sdentry);
		break;
	case -ERESTARTSYS:
		/* The result of the rename is unknown. Play it safe by
		 * forcing a new lookup */
		d_drop(dentry);
		d_drop(sdentry);
	}
515 516
	rpc_put_task(task);
out_dput:
T
Trond Myklebust 已提交
517
	iput(inode);
518 519 520 521
	dput(sdentry);
out:
	return error;
}