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