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