xref: /openbmc/linux/fs/nfs/unlink.c (revision 70ded201)
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_block_sillyrename(struct dentry *dentry)
211 {
212 	struct nfs_inode *nfsi = NFS_I(dentry->d_inode);
213 
214 	wait_event(nfsi->waitqueue, atomic_cmpxchg(&nfsi->silly_count, 1, 0) == 1);
215 }
216 
217 void nfs_unblock_sillyrename(struct dentry *dentry)
218 {
219 	struct inode *dir = dentry->d_inode;
220 	struct nfs_inode *nfsi = NFS_I(dir);
221 	struct nfs_unlinkdata *data;
222 
223 	atomic_inc(&nfsi->silly_count);
224 	spin_lock(&dir->i_lock);
225 	while (!hlist_empty(&nfsi->silly_list)) {
226 		if (!atomic_inc_not_zero(&nfsi->silly_count))
227 			break;
228 		data = hlist_entry(nfsi->silly_list.first, struct nfs_unlinkdata, list);
229 		hlist_del(&data->list);
230 		spin_unlock(&dir->i_lock);
231 		if (nfs_do_call_unlink(dentry, dir, data) == 0)
232 			nfs_free_unlinkdata(data);
233 		spin_lock(&dir->i_lock);
234 	}
235 	spin_unlock(&dir->i_lock);
236 }
237 
238 /**
239  * nfs_async_unlink - asynchronous unlinking of a file
240  * @dir: parent directory of dentry
241  * @dentry: dentry to unlink
242  */
243 static int
244 nfs_async_unlink(struct inode *dir, struct dentry *dentry)
245 {
246 	struct nfs_unlinkdata *data;
247 	int status = -ENOMEM;
248 	void *devname_garbage = NULL;
249 
250 	data = kzalloc(sizeof(*data), GFP_KERNEL);
251 	if (data == NULL)
252 		goto out;
253 
254 	data->cred = rpc_lookup_cred();
255 	if (IS_ERR(data->cred)) {
256 		status = PTR_ERR(data->cred);
257 		goto out_free;
258 	}
259 	data->res.dir_attr = &data->dir_attr;
260 
261 	status = -EBUSY;
262 	spin_lock(&dentry->d_lock);
263 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
264 		goto out_unlock;
265 	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
266 	devname_garbage = dentry->d_fsdata;
267 	dentry->d_fsdata = data;
268 	spin_unlock(&dentry->d_lock);
269 	/*
270 	 * If we'd displaced old cached devname, free it.  At that
271 	 * point dentry is definitely not a root, so we won't need
272 	 * that anymore.
273 	 */
274 	kfree(devname_garbage);
275 	return 0;
276 out_unlock:
277 	spin_unlock(&dentry->d_lock);
278 	put_rpccred(data->cred);
279 out_free:
280 	kfree(data);
281 out:
282 	return status;
283 }
284 
285 /**
286  * nfs_complete_unlink - Initialize completion of the sillydelete
287  * @dentry: dentry to delete
288  * @inode: inode
289  *
290  * Since we're most likely to be called by dentry_iput(), we
291  * only use the dentry to find the sillydelete. We then copy the name
292  * into the qstr.
293  */
294 void
295 nfs_complete_unlink(struct dentry *dentry, struct inode *inode)
296 {
297 	struct nfs_unlinkdata	*data = NULL;
298 
299 	spin_lock(&dentry->d_lock);
300 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
301 		dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
302 		data = dentry->d_fsdata;
303 		dentry->d_fsdata = NULL;
304 	}
305 	spin_unlock(&dentry->d_lock);
306 
307 	if (data != NULL && (NFS_STALE(inode) || !nfs_call_unlink(dentry, data)))
308 		nfs_free_unlinkdata(data);
309 }
310 
311 /* Cancel a queued async unlink. Called when a sillyrename run fails. */
312 static void
313 nfs_cancel_async_unlink(struct dentry *dentry)
314 {
315 	spin_lock(&dentry->d_lock);
316 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
317 		struct nfs_unlinkdata *data = dentry->d_fsdata;
318 
319 		dentry->d_flags &= ~DCACHE_NFSFS_RENAMED;
320 		dentry->d_fsdata = NULL;
321 		spin_unlock(&dentry->d_lock);
322 		nfs_free_unlinkdata(data);
323 		return;
324 	}
325 	spin_unlock(&dentry->d_lock);
326 }
327 
328 /**
329  * nfs_async_rename_done - Sillyrename post-processing
330  * @task: rpc_task of the sillyrename
331  * @calldata: nfs_renamedata for the sillyrename
332  *
333  * Do the directory attribute updates and the d_move
334  */
335 static void nfs_async_rename_done(struct rpc_task *task, void *calldata)
336 {
337 	struct nfs_renamedata *data = calldata;
338 	struct inode *old_dir = data->old_dir;
339 	struct inode *new_dir = data->new_dir;
340 	struct dentry *old_dentry = data->old_dentry;
341 
342 	trace_nfs_sillyrename_rename(old_dir, old_dentry,
343 			new_dir, data->new_dentry, task->tk_status);
344 	if (!NFS_PROTO(old_dir)->rename_done(task, old_dir, new_dir)) {
345 		rpc_restart_call_prepare(task);
346 		return;
347 	}
348 
349 	if (task->tk_status != 0)
350 		nfs_cancel_async_unlink(old_dentry);
351 }
352 
353 /**
354  * nfs_async_rename_release - Release the sillyrename data.
355  * @calldata: the struct nfs_renamedata to be released
356  */
357 static void nfs_async_rename_release(void *calldata)
358 {
359 	struct nfs_renamedata	*data = calldata;
360 	struct super_block *sb = data->old_dir->i_sb;
361 
362 	if (data->old_dentry->d_inode)
363 		nfs_mark_for_revalidate(data->old_dentry->d_inode);
364 
365 	dput(data->old_dentry);
366 	dput(data->new_dentry);
367 	iput(data->old_dir);
368 	iput(data->new_dir);
369 	nfs_sb_deactive(sb);
370 	put_rpccred(data->cred);
371 	kfree(data);
372 }
373 
374 static void nfs_rename_prepare(struct rpc_task *task, void *calldata)
375 {
376 	struct nfs_renamedata *data = calldata;
377 	NFS_PROTO(data->old_dir)->rename_rpc_prepare(task, data);
378 }
379 
380 static const struct rpc_call_ops nfs_rename_ops = {
381 	.rpc_call_done = nfs_async_rename_done,
382 	.rpc_release = nfs_async_rename_release,
383 	.rpc_call_prepare = nfs_rename_prepare,
384 };
385 
386 /**
387  * nfs_async_rename - perform an asynchronous rename operation
388  * @old_dir: directory that currently holds the dentry to be renamed
389  * @new_dir: target directory for the rename
390  * @old_dentry: original dentry to be renamed
391  * @new_dentry: dentry to which the old_dentry should be renamed
392  *
393  * It's expected that valid references to the dentries and inodes are held
394  */
395 static struct rpc_task *
396 nfs_async_rename(struct inode *old_dir, struct inode *new_dir,
397 		 struct dentry *old_dentry, struct dentry *new_dentry)
398 {
399 	struct nfs_renamedata *data;
400 	struct rpc_message msg = { };
401 	struct rpc_task_setup task_setup_data = {
402 		.rpc_message = &msg,
403 		.callback_ops = &nfs_rename_ops,
404 		.workqueue = nfsiod_workqueue,
405 		.rpc_client = NFS_CLIENT(old_dir),
406 		.flags = RPC_TASK_ASYNC,
407 	};
408 
409 	data = kzalloc(sizeof(*data), GFP_KERNEL);
410 	if (data == NULL)
411 		return ERR_PTR(-ENOMEM);
412 	task_setup_data.callback_data = data;
413 
414 	data->cred = rpc_lookup_cred();
415 	if (IS_ERR(data->cred)) {
416 		struct rpc_task *task = ERR_CAST(data->cred);
417 		kfree(data);
418 		return task;
419 	}
420 
421 	msg.rpc_argp = &data->args;
422 	msg.rpc_resp = &data->res;
423 	msg.rpc_cred = data->cred;
424 
425 	/* set up nfs_renamedata */
426 	data->old_dir = old_dir;
427 	ihold(old_dir);
428 	data->new_dir = new_dir;
429 	ihold(new_dir);
430 	data->old_dentry = dget(old_dentry);
431 	data->new_dentry = dget(new_dentry);
432 	nfs_fattr_init(&data->old_fattr);
433 	nfs_fattr_init(&data->new_fattr);
434 
435 	/* set up nfs_renameargs */
436 	data->args.old_dir = NFS_FH(old_dir);
437 	data->args.old_name = &old_dentry->d_name;
438 	data->args.new_dir = NFS_FH(new_dir);
439 	data->args.new_name = &new_dentry->d_name;
440 
441 	/* set up nfs_renameres */
442 	data->res.old_fattr = &data->old_fattr;
443 	data->res.new_fattr = &data->new_fattr;
444 
445 	nfs_sb_active(old_dir->i_sb);
446 
447 	NFS_PROTO(data->old_dir)->rename_setup(&msg, old_dir);
448 
449 	return rpc_run_task(&task_setup_data);
450 }
451 
452 #define SILLYNAME_PREFIX ".nfs"
453 #define SILLYNAME_PREFIX_LEN ((unsigned)sizeof(SILLYNAME_PREFIX) - 1)
454 #define SILLYNAME_FILEID_LEN ((unsigned)sizeof(u64) << 1)
455 #define SILLYNAME_COUNTER_LEN ((unsigned)sizeof(unsigned int) << 1)
456 #define SILLYNAME_LEN (SILLYNAME_PREFIX_LEN + \
457 		SILLYNAME_FILEID_LEN + \
458 		SILLYNAME_COUNTER_LEN)
459 
460 /**
461  * nfs_sillyrename - Perform a silly-rename of a dentry
462  * @dir: inode of directory that contains dentry
463  * @dentry: dentry to be sillyrenamed
464  *
465  * NFSv2/3 is stateless and the server doesn't know when the client is
466  * holding a file open. To prevent application problems when a file is
467  * unlinked while it's still open, the client performs a "silly-rename".
468  * That is, it renames the file to a hidden file in the same directory,
469  * and only performs the unlink once the last reference to it is put.
470  *
471  * The final cleanup is done during dentry_iput.
472  *
473  * (Note: NFSv4 is stateful, and has opens, so in theory an NFSv4 server
474  * could take responsibility for keeping open files referenced.  The server
475  * would also need to ensure that opened-but-deleted files were kept over
476  * reboots.  However, we may not assume a server does so.  (RFC 5661
477  * does provide an OPEN4_RESULT_PRESERVE_UNLINKED flag that a server can
478  * use to advertise that it does this; some day we may take advantage of
479  * it.))
480  */
481 int
482 nfs_sillyrename(struct inode *dir, struct dentry *dentry)
483 {
484 	static unsigned int sillycounter;
485 	unsigned char silly[SILLYNAME_LEN + 1];
486 	unsigned long long fileid;
487 	struct dentry *sdentry;
488 	struct rpc_task *task;
489 	int            error = -EIO;
490 
491 	dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
492 		dentry->d_parent->d_name.name, dentry->d_name.name,
493 		d_count(dentry));
494 	nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
495 
496 	/*
497 	 * We don't allow a dentry to be silly-renamed twice.
498 	 */
499 	error = -EBUSY;
500 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
501 		goto out;
502 
503 	fileid = NFS_FILEID(dentry->d_inode);
504 
505 	/* Return delegation in anticipation of the rename */
506 	NFS_PROTO(dentry->d_inode)->return_delegation(dentry->d_inode);
507 
508 	sdentry = NULL;
509 	do {
510 		int slen;
511 		dput(sdentry);
512 		sillycounter++;
513 		slen = scnprintf(silly, sizeof(silly),
514 				SILLYNAME_PREFIX "%0*llx%0*x",
515 				SILLYNAME_FILEID_LEN, fileid,
516 				SILLYNAME_COUNTER_LEN, sillycounter);
517 
518 		dfprintk(VFS, "NFS: trying to rename %s to %s\n",
519 				dentry->d_name.name, silly);
520 
521 		sdentry = lookup_one_len(silly, dentry->d_parent, slen);
522 		/*
523 		 * N.B. Better to return EBUSY here ... it could be
524 		 * dangerous to delete the file while it's in use.
525 		 */
526 		if (IS_ERR(sdentry))
527 			goto out;
528 	} while (sdentry->d_inode != NULL); /* need negative lookup */
529 
530 	/* queue unlink first. Can't do this from rpc_release as it
531 	 * has to allocate memory
532 	 */
533 	error = nfs_async_unlink(dir, dentry);
534 	if (error)
535 		goto out_dput;
536 
537 	/* populate unlinkdata with the right dname */
538 	error = nfs_copy_dname(sdentry,
539 				(struct nfs_unlinkdata *)dentry->d_fsdata);
540 	if (error) {
541 		nfs_cancel_async_unlink(dentry);
542 		goto out_dput;
543 	}
544 
545 	/* run the rename task, undo unlink if it fails */
546 	task = nfs_async_rename(dir, dir, dentry, sdentry);
547 	if (IS_ERR(task)) {
548 		error = -EBUSY;
549 		nfs_cancel_async_unlink(dentry);
550 		goto out_dput;
551 	}
552 
553 	/* wait for the RPC task to complete, unless a SIGKILL intervenes */
554 	error = rpc_wait_for_completion_task(task);
555 	if (error == 0)
556 		error = task->tk_status;
557 	switch (error) {
558 	case 0:
559 		/* The rename succeeded */
560 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
561 		d_move(dentry, sdentry);
562 		break;
563 	case -ERESTARTSYS:
564 		/* The result of the rename is unknown. Play it safe by
565 		 * forcing a new lookup */
566 		d_drop(dentry);
567 		d_drop(sdentry);
568 	}
569 	rpc_put_task(task);
570 out_dput:
571 	dput(sdentry);
572 out:
573 	return error;
574 }
575