xref: /openbmc/linux/fs/nfs/unlink.c (revision 0e862a40)
1 /*
2  *  linux/fs/nfs/unlink.c
3  *
4  * nfs sillydelete handling
5  *
6  */
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>
14 #include <linux/sched.h>
15 #include <linux/wait.h>
16 #include <linux/namei.h>
17 
18 #include "internal.h"
19 #include "nfs4_fs.h"
20 #include "iostat.h"
21 #include "delegation.h"
22 
23 #include "nfstrace.h"
24 
25 /**
26  * nfs_free_unlinkdata - release data from a sillydelete operation.
27  * @data: pointer to unlink structure.
28  */
29 static void
30 nfs_free_unlinkdata(struct nfs_unlinkdata *data)
31 {
32 	iput(data->dir);
33 	put_rpccred(data->cred);
34 	kfree(data->args.name.name);
35 	kfree(data);
36 }
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  */
44 static int nfs_copy_dname(struct dentry *dentry, struct nfs_unlinkdata *data)
45 {
46 	char		*str;
47 	int		len = dentry->d_name.len;
48 
49 	str = kmemdup(dentry->d_name.name, NAME_ALLOC_LEN(len), GFP_KERNEL);
50 	if (!str)
51 		return -ENOMEM;
52 	data->args.name.len = len;
53 	data->args.name.name = str;
54 	return 0;
55 }
56 
57 static 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 
64 static 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 
71 /**
72  * nfs_async_unlink_done - Sillydelete post-processing
73  * @task: rpc_task of the sillydelete
74  *
75  * Do the directory attribute update.
76  */
77 static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
78 {
79 	struct nfs_unlinkdata *data = calldata;
80 	struct inode *dir = data->dir;
81 
82 	trace_nfs_sillyrename_unlink(data, task->tk_status);
83 	if (!NFS_PROTO(dir)->unlink_done(task, dir))
84 		rpc_restart_call_prepare(task);
85 }
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  */
94 static void nfs_async_unlink_release(void *calldata)
95 {
96 	struct nfs_unlinkdata	*data = calldata;
97 	struct super_block *sb = data->dir->i_sb;
98 
99 	nfs_dec_sillycount(data->dir);
100 	nfs_free_unlinkdata(data);
101 	nfs_sb_deactive(sb);
102 }
103 
104 static void nfs_unlink_prepare(struct rpc_task *task, void *calldata)
105 {
106 	struct nfs_unlinkdata *data = calldata;
107 	NFS_PROTO(data->dir)->unlink_rpc_prepare(task, data);
108 }
109 
110 static const struct rpc_call_ops nfs_unlink_ops = {
111 	.rpc_call_done = nfs_async_unlink_done,
112 	.rpc_release = nfs_async_unlink_release,
113 	.rpc_call_prepare = nfs_unlink_prepare,
114 };
115 
116 static int nfs_do_call_unlink(struct dentry *parent, struct inode *dir, struct nfs_unlinkdata *data)
117 {
118 	struct rpc_message msg = {
119 		.rpc_argp = &data->args,
120 		.rpc_resp = &data->res,
121 		.rpc_cred = data->cred,
122 	};
123 	struct rpc_task_setup task_setup_data = {
124 		.rpc_message = &msg,
125 		.callback_ops = &nfs_unlink_ops,
126 		.callback_data = data,
127 		.workqueue = nfsiod_workqueue,
128 		.flags = RPC_TASK_ASYNC,
129 	};
130 	struct rpc_task *task;
131 	struct dentry *alias;
132 
133 	alias = d_lookup(parent, &data->args.name);
134 	if (alias != NULL) {
135 		int ret;
136 		void *devname_garbage = NULL;
137 
138 		/*
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);
143 		ret = nfs_copy_dname(alias, data);
144 		spin_lock(&alias->d_lock);
145 		if (ret == 0 && alias->d_inode != NULL &&
146 		    !(alias->d_flags & DCACHE_NFSFS_RENAMED)) {
147 			devname_garbage = alias->d_fsdata;
148 			alias->d_fsdata = data;
149 			alias->d_flags |= DCACHE_NFSFS_RENAMED;
150 			ret = 1;
151 		} else
152 			ret = 0;
153 		spin_unlock(&alias->d_lock);
154 		nfs_dec_sillycount(dir);
155 		dput(alias);
156 		/*
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 		 */
161 		kfree(devname_garbage);
162 		return ret;
163 	}
164 	data->dir = igrab(dir);
165 	if (!data->dir) {
166 		nfs_dec_sillycount(dir);
167 		return 0;
168 	}
169 	nfs_sb_active(dir->i_sb);
170 	data->args.fh = NFS_FH(dir);
171 	nfs_fattr_init(data->res.dir_attr);
172 
173 	NFS_PROTO(dir)->unlink_setup(&msg, dir);
174 
175 	task_setup_data.rpc_client = NFS_CLIENT(dir);
176 	task = rpc_run_task(&task_setup_data);
177 	if (!IS_ERR(task))
178 		rpc_put_task_async(task);
179 	return 1;
180 }
181 
182 static 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;
193 	/* 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);
204 out_dput:
205 	dput(parent);
206 out_free:
207 	return ret;
208 }
209 
210 void 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 
217 void 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 
224 void 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);
243 }
244 
245 /**
246  * nfs_async_unlink - asynchronous unlinking of a file
247  * @dir: parent directory of dentry
248  * @dentry: dentry to unlink
249  */
250 static int
251 nfs_async_unlink(struct inode *dir, struct dentry *dentry)
252 {
253 	struct nfs_unlinkdata *data;
254 	int status = -ENOMEM;
255 	void *devname_garbage = NULL;
256 
257 	data = kzalloc(sizeof(*data), GFP_KERNEL);
258 	if (data == NULL)
259 		goto out;
260 
261 	data->cred = rpc_lookup_cred();
262 	if (IS_ERR(data->cred)) {
263 		status = PTR_ERR(data->cred);
264 		goto out_free;
265 	}
266 	data->res.dir_attr = &data->dir_attr;
267 
268 	status = -EBUSY;
269 	spin_lock(&dentry->d_lock);
270 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
271 		goto out_unlock;
272 	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
273 	devname_garbage = dentry->d_fsdata;
274 	dentry->d_fsdata = data;
275 	spin_unlock(&dentry->d_lock);
276 	/*
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 	 */
281 	kfree(devname_garbage);
282 	return 0;
283 out_unlock:
284 	spin_unlock(&dentry->d_lock);
285 	put_rpccred(data->cred);
286 out_free:
287 	kfree(data);
288 out:
289 	return status;
290 }
291 
292 /**
293  * nfs_complete_unlink - Initialize completion of the sillydelete
294  * @dentry: dentry to delete
295  * @inode: inode
296  *
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  */
301 void
302 nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
303 {
304 	struct nfs_unlinkdata	*data = NULL;
305 
306 	spin_lock(&dentry->d_lock);
307 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
308 		dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
309 		data = dentry->d_fsdata;
310 		dentry->d_fsdata = NULL;
311 	}
312 	spin_unlock(&dentry->d_lock);
313 
314 	if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
315 		nfs_free_unlinkdata(data);
316 }
317 
318 /* Cancel a queued async unlink. Called when a sillyrename run fails. */
319 static void
320 nfs_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;
327 		dentry->d_fsdata = NULL;
328 		spin_unlock(&dentry->d_lock);
329 		nfs_free_unlinkdata(data);
330 		return;
331 	}
332 	spin_unlock(&dentry->d_lock);
333 }
334 
335 /**
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  */
342 static 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;
347 	struct dentry *old_dentry = data->old_dentry;
348 
349 	trace_nfs_sillyrename_rename(old_dir, old_dentry,
350 			new_dir, data->new_dentry, task->tk_status);
351 	if (!NFS_PROTO(old_dir)->rename_done(task, old_dir, new_dir)) {
352 		rpc_restart_call_prepare(task);
353 		return;
354 	}
355 
356 	if (data->complete)
357 		data->complete(task, data);
358 }
359 
360 /**
361  * nfs_async_rename_release - Release the sillyrename data.
362  * @calldata: the struct nfs_renamedata to be released
363  */
364 static 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 
381 static void nfs_rename_prepare(struct rpc_task *task, void *calldata)
382 {
383 	struct nfs_renamedata *data = calldata;
384 	NFS_PROTO(data->old_dir)->rename_rpc_prepare(task, data);
385 }
386 
387 static const struct rpc_call_ops nfs_rename_ops = {
388 	.rpc_call_done = nfs_async_rename_done,
389 	.rpc_release = nfs_async_rename_release,
390 	.rpc_call_prepare = nfs_rename_prepare,
391 };
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  */
402 struct rpc_task *
403 nfs_async_rename(struct inode *old_dir, struct inode *new_dir,
404 		 struct dentry *old_dentry, struct dentry *new_dentry,
405 		 void (*complete)(struct rpc_task *, struct nfs_renamedata *))
406 {
407 	struct nfs_renamedata *data;
408 	struct rpc_message msg = { };
409 	struct rpc_task_setup task_setup_data = {
410 		.rpc_message = &msg,
411 		.callback_ops = &nfs_rename_ops,
412 		.workqueue = nfsiod_workqueue,
413 		.rpc_client = NFS_CLIENT(old_dir),
414 		.flags = RPC_TASK_ASYNC,
415 	};
416 
417 	data = kzalloc(sizeof(*data), GFP_KERNEL);
418 	if (data == NULL)
419 		return ERR_PTR(-ENOMEM);
420 	task_setup_data.callback_data = data;
421 
422 	data->cred = rpc_lookup_cred();
423 	if (IS_ERR(data->cred)) {
424 		struct rpc_task *task = ERR_CAST(data->cred);
425 		kfree(data);
426 		return task;
427 	}
428 
429 	msg.rpc_argp = &data->args;
430 	msg.rpc_resp = &data->res;
431 	msg.rpc_cred = data->cred;
432 
433 	/* set up nfs_renamedata */
434 	data->old_dir = old_dir;
435 	ihold(old_dir);
436 	data->new_dir = new_dir;
437 	ihold(new_dir);
438 	data->old_dentry = dget(old_dentry);
439 	data->new_dentry = dget(new_dentry);
440 	nfs_fattr_init(&data->old_fattr);
441 	nfs_fattr_init(&data->new_fattr);
442 	data->complete = complete;
443 
444 	/* set up nfs_renameargs */
445 	data->args.old_dir = NFS_FH(old_dir);
446 	data->args.old_name = &old_dentry->d_name;
447 	data->args.new_dir = NFS_FH(new_dir);
448 	data->args.new_name = &new_dentry->d_name;
449 
450 	/* set up nfs_renameres */
451 	data->res.old_fattr = &data->old_fattr;
452 	data->res.new_fattr = &data->new_fattr;
453 
454 	nfs_sb_active(old_dir->i_sb);
455 
456 	NFS_PROTO(data->old_dir)->rename_setup(&msg, old_dir);
457 
458 	return rpc_run_task(&task_setup_data);
459 }
460 
461 /*
462  * Perform tasks needed when a sillyrename is done such as cancelling the
463  * queued async unlink if it failed.
464  */
465 static void
466 nfs_complete_sillyrename(struct rpc_task *task, struct nfs_renamedata *data)
467 {
468 	if (task->tk_status != 0)
469 		nfs_cancel_async_unlink(data->old_dentry);
470 }
471 
472 #define SILLYNAME_PREFIX ".nfs"
473 #define SILLYNAME_PREFIX_LEN ((unsigned)sizeof(SILLYNAME_PREFIX) - 1)
474 #define SILLYNAME_FILEID_LEN ((unsigned)sizeof(u64) << 1)
475 #define SILLYNAME_COUNTER_LEN ((unsigned)sizeof(unsigned int) << 1)
476 #define SILLYNAME_LEN (SILLYNAME_PREFIX_LEN + \
477 		SILLYNAME_FILEID_LEN + \
478 		SILLYNAME_COUNTER_LEN)
479 
480 /**
481  * nfs_sillyrename - Perform a silly-rename of a dentry
482  * @dir: inode of directory that contains dentry
483  * @dentry: dentry to be sillyrenamed
484  *
485  * NFSv2/3 is stateless and the server doesn't know when the client is
486  * holding a file open. To prevent application problems when a file is
487  * unlinked while it's still open, the client performs a "silly-rename".
488  * That is, it renames the file to a hidden file in the same directory,
489  * and only performs the unlink once the last reference to it is put.
490  *
491  * The final cleanup is done during dentry_iput.
492  *
493  * (Note: NFSv4 is stateful, and has opens, so in theory an NFSv4 server
494  * could take responsibility for keeping open files referenced.  The server
495  * would also need to ensure that opened-but-deleted files were kept over
496  * reboots.  However, we may not assume a server does so.  (RFC 5661
497  * does provide an OPEN4_RESULT_PRESERVE_UNLINKED flag that a server can
498  * use to advertise that it does this; some day we may take advantage of
499  * it.))
500  */
501 int
502 nfs_sillyrename(struct inode *dir, struct dentry *dentry)
503 {
504 	static unsigned int sillycounter;
505 	unsigned char silly[SILLYNAME_LEN + 1];
506 	unsigned long long fileid;
507 	struct dentry *sdentry;
508 	struct rpc_task *task;
509 	int            error = -EBUSY;
510 
511 	dfprintk(VFS, "NFS: silly-rename(%pd2, ct=%d)\n",
512 		dentry, d_count(dentry));
513 	nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
514 
515 	/*
516 	 * We don't allow a dentry to be silly-renamed twice.
517 	 */
518 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
519 		goto out;
520 
521 	fileid = NFS_FILEID(dentry->d_inode);
522 
523 	/* Return delegation in anticipation of the rename */
524 	NFS_PROTO(dentry->d_inode)->return_delegation(dentry->d_inode);
525 
526 	sdentry = NULL;
527 	do {
528 		int slen;
529 		dput(sdentry);
530 		sillycounter++;
531 		slen = scnprintf(silly, sizeof(silly),
532 				SILLYNAME_PREFIX "%0*llx%0*x",
533 				SILLYNAME_FILEID_LEN, fileid,
534 				SILLYNAME_COUNTER_LEN, sillycounter);
535 
536 		dfprintk(VFS, "NFS: trying to rename %pd to %s\n",
537 				dentry, silly);
538 
539 		sdentry = lookup_one_len(silly, dentry->d_parent, slen);
540 		/*
541 		 * N.B. Better to return EBUSY here ... it could be
542 		 * dangerous to delete the file while it's in use.
543 		 */
544 		if (IS_ERR(sdentry))
545 			goto out;
546 	} while (sdentry->d_inode != NULL); /* need negative lookup */
547 
548 	/* queue unlink first. Can't do this from rpc_release as it
549 	 * has to allocate memory
550 	 */
551 	error = nfs_async_unlink(dir, dentry);
552 	if (error)
553 		goto out_dput;
554 
555 	/* populate unlinkdata with the right dname */
556 	error = nfs_copy_dname(sdentry,
557 				(struct nfs_unlinkdata *)dentry->d_fsdata);
558 	if (error) {
559 		nfs_cancel_async_unlink(dentry);
560 		goto out_dput;
561 	}
562 
563 	/* run the rename task, undo unlink if it fails */
564 	task = nfs_async_rename(dir, dir, dentry, sdentry,
565 					nfs_complete_sillyrename);
566 	if (IS_ERR(task)) {
567 		error = -EBUSY;
568 		nfs_cancel_async_unlink(dentry);
569 		goto out_dput;
570 	}
571 
572 	/* wait for the RPC task to complete, unless a SIGKILL intervenes */
573 	error = rpc_wait_for_completion_task(task);
574 	if (error == 0)
575 		error = task->tk_status;
576 	switch (error) {
577 	case 0:
578 		/* The rename succeeded */
579 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
580 		d_move(dentry, sdentry);
581 		break;
582 	case -ERESTARTSYS:
583 		/* The result of the rename is unknown. Play it safe by
584 		 * forcing a new lookup */
585 		d_drop(dentry);
586 		d_drop(sdentry);
587 	}
588 	rpc_put_task(task);
589 out_dput:
590 	dput(sdentry);
591 out:
592 	return error;
593 }
594