]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - fs/aufs/i_op_ren.c
UBUNTU: SAUCE: Update aufs to 5.4.3 20200302
[mirror_ubuntu-focal-kernel.git] / fs / aufs / i_op_ren.c
CommitLineData
a3a49a17
SF
1// SPDX-License-Identifier: GPL-2.0
2/*
e4a3f096 3 * Copyright (C) 2005-2020 Junjiro R. Okajima
a3a49a17
SF
4 *
5 * This program, aufs is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/*
20 * inode operation (rename entry)
21 * todo: this is crazy monster
22 */
23
24#include <linux/iversion.h>
25#include "aufs.h"
26
27enum { AuSRC, AuDST, AuSrcDst };
28enum { AuPARENT, AuCHILD, AuParentChild };
29
30#define AuRen_ISDIR_SRC 1
31#define AuRen_ISDIR_DST (1 << 1)
32#define AuRen_ISSAMEDIR (1 << 2)
33#define AuRen_WHSRC (1 << 3)
34#define AuRen_WHDST (1 << 4)
35#define AuRen_MNT_WRITE (1 << 5)
36#define AuRen_DT_DSTDIR (1 << 6)
37#define AuRen_DIROPQ_SRC (1 << 7)
38#define AuRen_DIROPQ_DST (1 << 8)
39#define AuRen_DIRREN (1 << 9)
40#define AuRen_DROPPED_SRC (1 << 10)
41#define AuRen_DROPPED_DST (1 << 11)
42#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
43#define au_fset_ren(flags, name) \
44 do { (flags) |= AuRen_##name; } while (0)
45#define au_fclr_ren(flags, name) \
46 do { (flags) &= ~AuRen_##name; } while (0)
47
48#ifndef CONFIG_AUFS_DIRREN
49#undef AuRen_DIRREN
50#define AuRen_DIRREN 0
51#endif
52
53struct au_ren_args {
54 struct {
55 struct dentry *dentry, *h_dentry, *parent, *h_parent,
56 *wh_dentry;
57 struct inode *dir, *inode;
58 struct au_hinode *hdir, *hinode;
59 struct au_dtime dt[AuParentChild];
60 aufs_bindex_t btop, bdiropq;
61 } sd[AuSrcDst];
62
63#define src_dentry sd[AuSRC].dentry
64#define src_dir sd[AuSRC].dir
65#define src_inode sd[AuSRC].inode
66#define src_h_dentry sd[AuSRC].h_dentry
67#define src_parent sd[AuSRC].parent
68#define src_h_parent sd[AuSRC].h_parent
69#define src_wh_dentry sd[AuSRC].wh_dentry
70#define src_hdir sd[AuSRC].hdir
71#define src_hinode sd[AuSRC].hinode
72#define src_h_dir sd[AuSRC].hdir->hi_inode
73#define src_dt sd[AuSRC].dt
74#define src_btop sd[AuSRC].btop
75#define src_bdiropq sd[AuSRC].bdiropq
76
77#define dst_dentry sd[AuDST].dentry
78#define dst_dir sd[AuDST].dir
79#define dst_inode sd[AuDST].inode
80#define dst_h_dentry sd[AuDST].h_dentry
81#define dst_parent sd[AuDST].parent
82#define dst_h_parent sd[AuDST].h_parent
83#define dst_wh_dentry sd[AuDST].wh_dentry
84#define dst_hdir sd[AuDST].hdir
85#define dst_hinode sd[AuDST].hinode
86#define dst_h_dir sd[AuDST].hdir->hi_inode
87#define dst_dt sd[AuDST].dt
88#define dst_btop sd[AuDST].btop
89#define dst_bdiropq sd[AuDST].bdiropq
90
91 struct dentry *h_trap;
92 struct au_branch *br;
93 struct path h_path;
94 struct au_nhash whlist;
95 aufs_bindex_t btgt, src_bwh;
96
97 struct {
98 unsigned short auren_flags;
99 unsigned char flags; /* syscall parameter */
100 unsigned char exchange;
101 } __packed;
102
103 struct au_whtmp_rmdir *thargs;
104 struct dentry *h_dst;
105 struct au_hinode *h_root;
106};
107
108/* ---------------------------------------------------------------------- */
109
110/*
111 * functions for reverting.
112 * when an error happened in a single rename systemcall, we should revert
113 * everything as if nothing happened.
114 * we don't need to revert the copied-up/down the parent dir since they are
115 * harmless.
116 */
117
118#define RevertFailure(fmt, ...) do { \
119 AuIOErr("revert failure: " fmt " (%d, %d)\n", \
120 ##__VA_ARGS__, err, rerr); \
121 err = -EIO; \
122} while (0)
123
124static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
125{
126 int rerr;
127 struct dentry *d;
128#define src_or_dst(member) a->sd[idx].member
129
130 d = src_or_dst(dentry); /* {src,dst}_dentry */
131 au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
132 rerr = au_diropq_remove(d, a->btgt);
133 au_hn_inode_unlock(src_or_dst(hinode));
134 au_set_dbdiropq(d, src_or_dst(bdiropq));
135 if (rerr)
136 RevertFailure("remove diropq %pd", d);
137
138#undef src_or_dst_
139}
140
141static void au_ren_rev_diropq(int err, struct au_ren_args *a)
142{
143 if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
144 au_ren_do_rev_diropq(err, a, AuSRC);
145 if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
146 au_ren_do_rev_diropq(err, a, AuDST);
147}
148
149static void au_ren_rev_rename(int err, struct au_ren_args *a)
150{
151 int rerr;
152 struct inode *delegated;
153
154 a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
155 a->src_h_parent);
156 rerr = PTR_ERR(a->h_path.dentry);
157 if (IS_ERR(a->h_path.dentry)) {
158 RevertFailure("lkup one %pd", a->src_dentry);
159 return;
160 }
161
162 delegated = NULL;
163 rerr = vfsub_rename(a->dst_h_dir,
164 au_h_dptr(a->src_dentry, a->btgt),
165 a->src_h_dir, &a->h_path, &delegated, a->flags);
166 if (unlikely(rerr == -EWOULDBLOCK)) {
167 pr_warn("cannot retry for NFSv4 delegation"
168 " for an internal rename\n");
169 iput(delegated);
170 }
171 d_drop(a->h_path.dentry);
172 dput(a->h_path.dentry);
173 /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
174 if (rerr)
175 RevertFailure("rename %pd", a->src_dentry);
176}
177
178static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
179{
180 int rerr;
181 struct inode *delegated;
182
183 a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
184 a->dst_h_parent);
185 rerr = PTR_ERR(a->h_path.dentry);
186 if (IS_ERR(a->h_path.dentry)) {
187 RevertFailure("lkup one %pd", a->dst_dentry);
188 return;
189 }
190 if (d_is_positive(a->h_path.dentry)) {
191 d_drop(a->h_path.dentry);
192 dput(a->h_path.dentry);
193 return;
194 }
195
196 delegated = NULL;
197 rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
198 &delegated, a->flags);
199 if (unlikely(rerr == -EWOULDBLOCK)) {
200 pr_warn("cannot retry for NFSv4 delegation"
201 " for an internal rename\n");
202 iput(delegated);
203 }
204 d_drop(a->h_path.dentry);
205 dput(a->h_path.dentry);
206 if (!rerr)
207 au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
208 else
209 RevertFailure("rename %pd", a->h_dst);
210}
211
212static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
213{
214 int rerr;
215
216 a->h_path.dentry = a->src_wh_dentry;
217 rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
218 au_set_dbwh(a->src_dentry, a->src_bwh);
219 if (rerr)
220 RevertFailure("unlink %pd", a->src_wh_dentry);
221}
222#undef RevertFailure
223
224/* ---------------------------------------------------------------------- */
225
226/*
227 * when we have to copyup the renaming entry, do it with the rename-target name
228 * in order to minimize the cost (the later actual rename is unnecessary).
229 * otherwise rename it on the target branch.
230 */
231static int au_ren_or_cpup(struct au_ren_args *a)
232{
233 int err;
234 struct dentry *d;
235 struct inode *delegated;
236
237 d = a->src_dentry;
238 if (au_dbtop(d) == a->btgt) {
239 a->h_path.dentry = a->dst_h_dentry;
240 AuDebugOn(au_dbtop(d) != a->btgt);
241 delegated = NULL;
242 err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
243 a->dst_h_dir, &a->h_path, &delegated,
244 a->flags);
245 if (unlikely(err == -EWOULDBLOCK)) {
246 pr_warn("cannot retry for NFSv4 delegation"
247 " for an internal rename\n");
248 iput(delegated);
249 }
250 } else
251 BUG();
252
253 if (!err && a->h_dst)
254 /* it will be set to dinfo later */
255 dget(a->h_dst);
256
257 return err;
258}
259
260/* cf. aufs_rmdir() */
261static int au_ren_del_whtmp(struct au_ren_args *a)
262{
263 int err;
264 struct inode *dir;
265
266 dir = a->dst_dir;
267 SiMustAnyLock(dir->i_sb);
268 if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
269 au_sbi(dir->i_sb)->si_dirwh)
270 || au_test_fs_remote(a->h_dst->d_sb)) {
271 err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
272 if (unlikely(err))
273 pr_warn("failed removing whtmp dir %pd (%d), "
274 "ignored.\n", a->h_dst, err);
275 } else {
276 au_nhash_wh_free(&a->thargs->whlist);
277 a->thargs->whlist = a->whlist;
278 a->whlist.nh_num = 0;
279 au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
280 dput(a->h_dst);
281 a->thargs = NULL;
282 }
283
284 return 0;
285}
286
287/* make it 'opaque' dir. */
288static int au_ren_do_diropq(struct au_ren_args *a, int idx)
289{
290 int err;
291 struct dentry *d, *diropq;
292#define src_or_dst(member) a->sd[idx].member
293
294 err = 0;
295 d = src_or_dst(dentry); /* {src,dst}_dentry */
296 src_or_dst(bdiropq) = au_dbdiropq(d);
297 src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
298 au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
299 diropq = au_diropq_create(d, a->btgt);
300 au_hn_inode_unlock(src_or_dst(hinode));
301 if (IS_ERR(diropq))
302 err = PTR_ERR(diropq);
303 else
304 dput(diropq);
305
306#undef src_or_dst_
307 return err;
308}
309
310static int au_ren_diropq(struct au_ren_args *a)
311{
312 int err;
313 unsigned char always;
314 struct dentry *d;
315
316 err = 0;
317 d = a->dst_dentry; /* already renamed on the branch */
318 always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
319 if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
320 && !au_ftest_ren(a->auren_flags, DIRREN)
321 && a->btgt != au_dbdiropq(a->src_dentry)
322 && (a->dst_wh_dentry
323 || a->btgt <= au_dbdiropq(d)
324 /* hide the lower to keep xino */
325 /* the lowers may not be a dir, but we hide them anyway */
326 || a->btgt < au_dbbot(d)
327 || always)) {
328 AuDbg("here\n");
329 err = au_ren_do_diropq(a, AuSRC);
330 if (unlikely(err))
331 goto out;
332 au_fset_ren(a->auren_flags, DIROPQ_SRC);
333 }
334 if (!a->exchange)
335 goto out; /* success */
336
337 d = a->src_dentry; /* already renamed on the branch */
338 if (au_ftest_ren(a->auren_flags, ISDIR_DST)
339 && a->btgt != au_dbdiropq(a->dst_dentry)
340 && (a->btgt < au_dbdiropq(d)
341 || a->btgt < au_dbbot(d)
342 || always)) {
343 AuDbgDentry(a->src_dentry);
344 AuDbgDentry(a->dst_dentry);
345 err = au_ren_do_diropq(a, AuDST);
346 if (unlikely(err))
347 goto out_rev_src;
348 au_fset_ren(a->auren_flags, DIROPQ_DST);
349 }
350 goto out; /* success */
351
352out_rev_src:
353 AuDbg("err %d, reverting src\n", err);
354 au_ren_rev_diropq(err, a);
355out:
356 return err;
357}
358
359static int do_rename(struct au_ren_args *a)
360{
361 int err;
362 struct dentry *d, *h_d;
363
364 if (!a->exchange) {
365 /* prepare workqueue args for asynchronous rmdir */
366 h_d = a->dst_h_dentry;
367 if (au_ftest_ren(a->auren_flags, ISDIR_DST)
368 /* && !au_ftest_ren(a->auren_flags, DIRREN) */
369 && d_is_positive(h_d)) {
370 err = -ENOMEM;
371 a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
372 GFP_NOFS);
373 if (unlikely(!a->thargs))
374 goto out;
375 a->h_dst = dget(h_d);
376 }
377
378 /* create whiteout for src_dentry */
379 if (au_ftest_ren(a->auren_flags, WHSRC)) {
380 a->src_bwh = au_dbwh(a->src_dentry);
381 AuDebugOn(a->src_bwh >= 0);
382 a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
383 a->src_h_parent);
384 err = PTR_ERR(a->src_wh_dentry);
385 if (IS_ERR(a->src_wh_dentry))
386 goto out_thargs;
387 }
388
389 /* lookup whiteout for dentry */
390 if (au_ftest_ren(a->auren_flags, WHDST)) {
391 h_d = au_wh_lkup(a->dst_h_parent,
392 &a->dst_dentry->d_name, a->br);
393 err = PTR_ERR(h_d);
394 if (IS_ERR(h_d))
395 goto out_whsrc;
396 if (d_is_negative(h_d))
397 dput(h_d);
398 else
399 a->dst_wh_dentry = h_d;
400 }
401
402 /* rename dentry to tmpwh */
403 if (a->thargs) {
404 err = au_whtmp_ren(a->dst_h_dentry, a->br);
405 if (unlikely(err))
406 goto out_whdst;
407
408 d = a->dst_dentry;
409 au_set_h_dptr(d, a->btgt, NULL);
410 err = au_lkup_neg(d, a->btgt, /*wh*/0);
411 if (unlikely(err))
412 goto out_whtmp;
413 a->dst_h_dentry = au_h_dptr(d, a->btgt);
414 }
415 }
416
417 BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
e4a3f096 418#if 0 /* debugging */
a3a49a17
SF
419 BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
420 && d_is_positive(a->dst_h_dentry)
421 && a->src_btop != a->btgt);
422#endif
423
424 /* rename by vfs_rename or cpup */
425 err = au_ren_or_cpup(a);
426 if (unlikely(err))
427 /* leave the copied-up one */
428 goto out_whtmp;
429
430 /* make dir opaque */
431 err = au_ren_diropq(a);
432 if (unlikely(err))
433 goto out_rename;
434
435 /* update target timestamps */
436 if (a->exchange) {
437 AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
438 a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
439 vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
440 a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
441 }
442 AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
443 a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
444 vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
445 a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
446
447 if (!a->exchange) {
448 /* remove whiteout for dentry */
449 if (a->dst_wh_dentry) {
450 a->h_path.dentry = a->dst_wh_dentry;
451 err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
452 a->dst_dentry);
453 if (unlikely(err))
454 goto out_diropq;
455 }
456
457 /* remove whtmp */
458 if (a->thargs)
459 au_ren_del_whtmp(a); /* ignore this error */
460
461 au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
462 }
463 err = 0;
464 goto out_success;
465
466out_diropq:
467 au_ren_rev_diropq(err, a);
468out_rename:
469 au_ren_rev_rename(err, a);
470 dput(a->h_dst);
471out_whtmp:
472 if (a->thargs)
473 au_ren_rev_whtmp(err, a);
474out_whdst:
475 dput(a->dst_wh_dentry);
476 a->dst_wh_dentry = NULL;
477out_whsrc:
478 if (a->src_wh_dentry)
479 au_ren_rev_whsrc(err, a);
480out_success:
481 dput(a->src_wh_dentry);
482 dput(a->dst_wh_dentry);
483out_thargs:
484 if (a->thargs) {
485 dput(a->h_dst);
486 au_whtmp_rmdir_free(a->thargs);
487 a->thargs = NULL;
488 }
489out:
490 return err;
491}
492
493/* ---------------------------------------------------------------------- */
494
495/*
496 * test if @dentry dir can be rename destination or not.
497 * success means, it is a logically empty dir.
498 */
499static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
500{
501 return au_test_empty(dentry, whlist);
502}
503
504/*
505 * test if @a->src_dentry dir can be rename source or not.
506 * if it can, return 0.
507 * success means,
508 * - it is a logically empty dir.
509 * - or, it exists on writable branch and has no children including whiteouts
510 * on the lower branch unless DIRREN is on.
511 */
512static int may_rename_srcdir(struct au_ren_args *a)
513{
514 int err;
515 unsigned int rdhash;
516 aufs_bindex_t btop, btgt;
517 struct dentry *dentry;
518 struct super_block *sb;
519 struct au_sbinfo *sbinfo;
520
521 dentry = a->src_dentry;
522 sb = dentry->d_sb;
523 sbinfo = au_sbi(sb);
524 if (au_opt_test(sbinfo->si_mntflags, DIRREN))
525 au_fset_ren(a->auren_flags, DIRREN);
526
527 btgt = a->btgt;
528 btop = au_dbtop(dentry);
529 if (btop != btgt) {
530 struct au_nhash whlist;
531
532 SiMustAnyLock(sb);
533 rdhash = sbinfo->si_rdhash;
534 if (!rdhash)
535 rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
536 dentry));
537 err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
538 if (unlikely(err))
539 goto out;
540 err = au_test_empty(dentry, &whlist);
541 au_nhash_wh_free(&whlist);
542 goto out;
543 }
544
545 if (btop == au_dbtaildir(dentry))
546 return 0; /* success */
547
548 err = au_test_empty_lower(dentry);
549
550out:
551 if (err == -ENOTEMPTY) {
552 if (au_ftest_ren(a->auren_flags, DIRREN)) {
553 err = 0;
554 } else {
555 AuWarn1("renaming dir who has child(ren) on multiple "
556 "branches, is not supported\n");
557 err = -EXDEV;
558 }
559 }
560 return err;
561}
562
563/* side effect: sets whlist and h_dentry */
564static int au_ren_may_dir(struct au_ren_args *a)
565{
566 int err;
567 unsigned int rdhash;
568 struct dentry *d;
569
570 d = a->dst_dentry;
571 SiMustAnyLock(d->d_sb);
572
573 err = 0;
574 if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
575 rdhash = au_sbi(d->d_sb)->si_rdhash;
576 if (!rdhash)
577 rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
578 err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
579 if (unlikely(err))
580 goto out;
581
582 if (!a->exchange) {
583 au_set_dbtop(d, a->dst_btop);
584 err = may_rename_dstdir(d, &a->whlist);
585 au_set_dbtop(d, a->btgt);
586 } else
587 err = may_rename_srcdir(a);
588 }
589 a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
590 if (unlikely(err))
591 goto out;
592
593 d = a->src_dentry;
594 a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
595 if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
596 err = may_rename_srcdir(a);
597 if (unlikely(err)) {
598 au_nhash_wh_free(&a->whlist);
599 a->whlist.nh_num = 0;
600 }
601 }
602out:
603 return err;
604}
605
606/* ---------------------------------------------------------------------- */
607
608/*
609 * simple tests for rename.
610 * following the checks in vfs, plus the parent-child relationship.
611 */
612static int au_may_ren(struct au_ren_args *a)
613{
614 int err, isdir;
615 struct inode *h_inode;
616
617 if (a->src_btop == a->btgt) {
618 err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
619 au_ftest_ren(a->auren_flags, ISDIR_SRC));
620 if (unlikely(err))
621 goto out;
622 err = -EINVAL;
623 if (unlikely(a->src_h_dentry == a->h_trap))
624 goto out;
625 }
626
627 err = 0;
628 if (a->dst_btop != a->btgt)
629 goto out;
630
631 err = -ENOTEMPTY;
632 if (unlikely(a->dst_h_dentry == a->h_trap))
633 goto out;
634
635 err = -EIO;
636 isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
637 if (d_really_is_negative(a->dst_dentry)) {
638 if (d_is_negative(a->dst_h_dentry))
639 err = au_may_add(a->dst_dentry, a->btgt,
640 a->dst_h_parent, isdir);
641 } else {
642 if (unlikely(d_is_negative(a->dst_h_dentry)))
643 goto out;
644 h_inode = d_inode(a->dst_h_dentry);
645 if (h_inode->i_nlink)
646 err = au_may_del(a->dst_dentry, a->btgt,
647 a->dst_h_parent, isdir);
648 }
649
650out:
651 if (unlikely(err == -ENOENT || err == -EEXIST))
652 err = -EIO;
653 AuTraceErr(err);
654 return err;
655}
656
657/* ---------------------------------------------------------------------- */
658
659/*
660 * locking order
661 * (VFS)
662 * - src_dir and dir by lock_rename()
663 * - inode if exists
664 * (aufs)
665 * - lock all
666 * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
667 * + si_read_lock
668 * + di_write_lock2_child()
669 * + di_write_lock_child()
670 * + ii_write_lock_child()
671 * + di_write_lock_child2()
672 * + ii_write_lock_child2()
673 * + src_parent and parent
674 * + di_write_lock_parent()
675 * + ii_write_lock_parent()
676 * + di_write_lock_parent2()
677 * + ii_write_lock_parent2()
678 * + lower src_dir and dir by vfsub_lock_rename()
679 * + verify the every relationships between child and parent. if any
680 * of them failed, unlock all and return -EBUSY.
681 */
682static void au_ren_unlock(struct au_ren_args *a)
683{
684 vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
685 a->dst_h_parent, a->dst_hdir);
686 if (au_ftest_ren(a->auren_flags, DIRREN)
687 && a->h_root)
688 au_hn_inode_unlock(a->h_root);
689 if (au_ftest_ren(a->auren_flags, MNT_WRITE))
690 vfsub_mnt_drop_write(au_br_mnt(a->br));
691}
692
693static int au_ren_lock(struct au_ren_args *a)
694{
695 int err;
696 unsigned int udba;
697
698 err = 0;
699 a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
700 a->src_hdir = au_hi(a->src_dir, a->btgt);
701 a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
702 a->dst_hdir = au_hi(a->dst_dir, a->btgt);
703
704 err = vfsub_mnt_want_write(au_br_mnt(a->br));
705 if (unlikely(err))
706 goto out;
707 au_fset_ren(a->auren_flags, MNT_WRITE);
708 if (au_ftest_ren(a->auren_flags, DIRREN)) {
709 struct dentry *root;
710 struct inode *dir;
711
712 /*
713 * sbinfo is already locked, so this ii_read_lock is
714 * unnecessary. but our debugging feature checks it.
715 */
716 root = a->src_inode->i_sb->s_root;
717 if (root != a->src_parent && root != a->dst_parent) {
718 dir = d_inode(root);
719 ii_read_lock_parent3(dir);
720 a->h_root = au_hi(dir, a->btgt);
721 ii_read_unlock(dir);
722 au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
723 }
724 }
725 a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
726 a->dst_h_parent, a->dst_hdir);
727 udba = au_opt_udba(a->src_dentry->d_sb);
728 if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
729 || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
730 err = au_busy_or_stale();
731 if (!err && au_dbtop(a->src_dentry) == a->btgt)
732 err = au_h_verify(a->src_h_dentry, udba,
733 d_inode(a->src_h_parent), a->src_h_parent,
734 a->br);
735 if (!err && au_dbtop(a->dst_dentry) == a->btgt)
736 err = au_h_verify(a->dst_h_dentry, udba,
737 d_inode(a->dst_h_parent), a->dst_h_parent,
738 a->br);
739 if (!err)
740 goto out; /* success */
741
742 err = au_busy_or_stale();
743 au_ren_unlock(a);
744
745out:
746 return err;
747}
748
749/* ---------------------------------------------------------------------- */
750
751static void au_ren_refresh_dir(struct au_ren_args *a)
752{
753 struct inode *dir;
754
755 dir = a->dst_dir;
756 inode_inc_iversion(dir);
757 if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
758 /* is this updating defined in POSIX? */
759 au_cpup_attr_timesizes(a->src_inode);
760 au_cpup_attr_nlink(dir, /*force*/1);
761 }
762 au_dir_ts(dir, a->btgt);
763
764 if (a->exchange) {
765 dir = a->src_dir;
766 inode_inc_iversion(dir);
767 if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
768 /* is this updating defined in POSIX? */
769 au_cpup_attr_timesizes(a->dst_inode);
770 au_cpup_attr_nlink(dir, /*force*/1);
771 }
772 au_dir_ts(dir, a->btgt);
773 }
774
775 if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
776 return;
777
778 dir = a->src_dir;
779 inode_inc_iversion(dir);
780 if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
781 au_cpup_attr_nlink(dir, /*force*/1);
782 au_dir_ts(dir, a->btgt);
783}
784
785static void au_ren_refresh(struct au_ren_args *a)
786{
787 aufs_bindex_t bbot, bindex;
788 struct dentry *d, *h_d;
789 struct inode *i, *h_i;
790 struct super_block *sb;
791
792 d = a->dst_dentry;
793 d_drop(d);
794 if (a->h_dst)
795 /* already dget-ed by au_ren_or_cpup() */
796 au_set_h_dptr(d, a->btgt, a->h_dst);
797
798 i = a->dst_inode;
799 if (i) {
800 if (!a->exchange) {
801 if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
802 vfsub_drop_nlink(i);
803 else {
804 vfsub_dead_dir(i);
805 au_cpup_attr_timesizes(i);
806 }
807 au_update_dbrange(d, /*do_put_zero*/1);
808 } else
809 au_cpup_attr_nlink(i, /*force*/1);
810 } else {
811 bbot = a->btgt;
812 for (bindex = au_dbtop(d); bindex < bbot; bindex++)
813 au_set_h_dptr(d, bindex, NULL);
814 bbot = au_dbbot(d);
815 for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
816 au_set_h_dptr(d, bindex, NULL);
817 au_update_dbrange(d, /*do_put_zero*/0);
818 }
819
820 if (a->exchange
821 || au_ftest_ren(a->auren_flags, DIRREN)) {
822 d_drop(a->src_dentry);
823 if (au_ftest_ren(a->auren_flags, DIRREN))
824 au_set_dbwh(a->src_dentry, -1);
825 return;
826 }
827
828 d = a->src_dentry;
829 au_set_dbwh(d, -1);
830 bbot = au_dbbot(d);
831 for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
832 h_d = au_h_dptr(d, bindex);
833 if (h_d)
834 au_set_h_dptr(d, bindex, NULL);
835 }
836 au_set_dbbot(d, a->btgt);
837
838 sb = d->d_sb;
839 i = a->src_inode;
840 if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
841 return; /* success */
842
843 bbot = au_ibbot(i);
844 for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
845 h_i = au_h_iptr(i, bindex);
846 if (h_i) {
847 au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
848 /* ignore this error */
849 au_set_h_iptr(i, bindex, NULL, 0);
850 }
851 }
852 au_set_ibbot(i, a->btgt);
853}
854
855/* ---------------------------------------------------------------------- */
856
857/* mainly for link(2) and rename(2) */
858int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
859{
860 aufs_bindex_t bdiropq, bwh;
861 struct dentry *parent;
862 struct au_branch *br;
863
864 parent = dentry->d_parent;
865 IMustLock(d_inode(parent)); /* dir is locked */
866
867 bdiropq = au_dbdiropq(parent);
868 bwh = au_dbwh(dentry);
869 br = au_sbr(dentry->d_sb, btgt);
870 if (au_br_rdonly(br)
871 || (0 <= bdiropq && bdiropq < btgt)
872 || (0 <= bwh && bwh < btgt))
873 btgt = -1;
874
875 AuDbg("btgt %d\n", btgt);
876 return btgt;
877}
878
879/* sets src_btop, dst_btop and btgt */
880static int au_ren_wbr(struct au_ren_args *a)
881{
882 int err;
883 struct au_wr_dir_args wr_dir_args = {
884 /* .force_btgt = -1, */
885 .flags = AuWrDir_ADD_ENTRY
886 };
887
888 a->src_btop = au_dbtop(a->src_dentry);
889 a->dst_btop = au_dbtop(a->dst_dentry);
890 if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
891 || au_ftest_ren(a->auren_flags, ISDIR_DST))
892 au_fset_wrdir(wr_dir_args.flags, ISDIR);
893 wr_dir_args.force_btgt = a->src_btop;
894 if (a->dst_inode && a->dst_btop < a->src_btop)
895 wr_dir_args.force_btgt = a->dst_btop;
896 wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
897 err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
898 a->btgt = err;
899 if (a->exchange)
900 au_update_dbtop(a->dst_dentry);
901
902 return err;
903}
904
905static void au_ren_dt(struct au_ren_args *a)
906{
907 a->h_path.dentry = a->src_h_parent;
908 au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
909 if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
910 a->h_path.dentry = a->dst_h_parent;
911 au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
912 }
913
914 au_fclr_ren(a->auren_flags, DT_DSTDIR);
915 if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
916 && !a->exchange)
917 return;
918
919 a->h_path.dentry = a->src_h_dentry;
920 au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
921 if (d_is_positive(a->dst_h_dentry)) {
922 au_fset_ren(a->auren_flags, DT_DSTDIR);
923 a->h_path.dentry = a->dst_h_dentry;
924 au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
925 }
926}
927
928static void au_ren_rev_dt(int err, struct au_ren_args *a)
929{
930 struct dentry *h_d;
931 struct inode *h_inode;
932
933 au_dtime_revert(a->src_dt + AuPARENT);
934 if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
935 au_dtime_revert(a->dst_dt + AuPARENT);
936
937 if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
938 h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
939 h_inode = d_inode(h_d);
940 inode_lock_nested(h_inode, AuLsc_I_CHILD);
941 au_dtime_revert(a->src_dt + AuCHILD);
942 inode_unlock(h_inode);
943
944 if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
945 h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
946 h_inode = d_inode(h_d);
947 inode_lock_nested(h_inode, AuLsc_I_CHILD);
948 au_dtime_revert(a->dst_dt + AuCHILD);
949 inode_unlock(h_inode);
950 }
951 }
952}
953
954/* ---------------------------------------------------------------------- */
955
956int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
957 struct inode *_dst_dir, struct dentry *_dst_dentry,
958 unsigned int _flags)
959{
960 int err, lock_flags;
961 void *rev;
962 /* reduce stack space */
963 struct au_ren_args *a;
964 struct au_pin pin;
965
966 AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
967 IMustLock(_src_dir);
968 IMustLock(_dst_dir);
969
970 err = -EINVAL;
971 if (unlikely(_flags & RENAME_WHITEOUT))
972 goto out;
973
974 err = -ENOMEM;
975 BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
976 a = kzalloc(sizeof(*a), GFP_NOFS);
977 if (unlikely(!a))
978 goto out;
979
980 a->flags = _flags;
981 BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
982 && RENAME_EXCHANGE > U8_MAX);
983 a->exchange = _flags & RENAME_EXCHANGE;
984 a->src_dir = _src_dir;
985 a->src_dentry = _src_dentry;
986 a->src_inode = NULL;
987 if (d_really_is_positive(a->src_dentry))
988 a->src_inode = d_inode(a->src_dentry);
989 a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
990 a->dst_dir = _dst_dir;
991 a->dst_dentry = _dst_dentry;
992 a->dst_inode = NULL;
993 if (d_really_is_positive(a->dst_dentry))
994 a->dst_inode = d_inode(a->dst_dentry);
995 a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
996 if (a->dst_inode) {
997 /*
998 * if EXCHANGE && src is non-dir && dst is dir,
999 * dst is not locked.
1000 */
1001 /* IMustLock(a->dst_inode); */
1002 au_igrab(a->dst_inode);
1003 }
1004
1005 err = -ENOTDIR;
1006 lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
1007 if (d_is_dir(a->src_dentry)) {
1008 au_fset_ren(a->auren_flags, ISDIR_SRC);
1009 if (unlikely(!a->exchange
1010 && d_really_is_positive(a->dst_dentry)
1011 && !d_is_dir(a->dst_dentry)))
1012 goto out_free;
1013 lock_flags |= AuLock_DIRS;
1014 }
1015 if (a->dst_inode && d_is_dir(a->dst_dentry)) {
1016 au_fset_ren(a->auren_flags, ISDIR_DST);
1017 if (unlikely(!a->exchange
1018 && d_really_is_positive(a->src_dentry)
1019 && !d_is_dir(a->src_dentry)))
1020 goto out_free;
1021 lock_flags |= AuLock_DIRS;
1022 }
1023 err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
1024 lock_flags);
1025 if (unlikely(err))
1026 goto out_free;
1027
1028 err = au_d_hashed_positive(a->src_dentry);
1029 if (unlikely(err))
1030 goto out_unlock;
1031 err = -ENOENT;
1032 if (a->dst_inode) {
1033 /*
1034 * If it is a dir, VFS unhash it before this
1035 * function. It means we cannot rely upon d_unhashed().
1036 */
1037 if (unlikely(!a->dst_inode->i_nlink))
1038 goto out_unlock;
1039 if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
1040 err = au_d_hashed_positive(a->dst_dentry);
1041 if (unlikely(err && !a->exchange))
1042 goto out_unlock;
1043 } else if (unlikely(IS_DEADDIR(a->dst_inode)))
1044 goto out_unlock;
1045 } else if (unlikely(d_unhashed(a->dst_dentry)))
1046 goto out_unlock;
1047
1048 /*
1049 * is it possible?
1050 * yes, it happened (in linux-3.3-rcN) but I don't know why.
1051 * there may exist a problem somewhere else.
1052 */
1053 err = -EINVAL;
1054 if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
1055 goto out_unlock;
1056
1057 au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
1058 di_write_lock_parent(a->dst_parent);
1059
1060 /* which branch we process */
1061 err = au_ren_wbr(a);
1062 if (unlikely(err < 0))
1063 goto out_parent;
1064 a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
1065 a->h_path.mnt = au_br_mnt(a->br);
1066
1067 /* are they available to be renamed */
1068 err = au_ren_may_dir(a);
1069 if (unlikely(err))
1070 goto out_children;
1071
1072 /* prepare the writable parent dir on the same branch */
1073 if (a->dst_btop == a->btgt) {
1074 au_fset_ren(a->auren_flags, WHDST);
1075 } else {
1076 err = au_cpup_dirs(a->dst_dentry, a->btgt);
1077 if (unlikely(err))
1078 goto out_children;
1079 }
1080
1081 err = 0;
1082 if (!a->exchange) {
1083 if (a->src_dir != a->dst_dir) {
1084 /*
1085 * this temporary unlock is safe,
1086 * because both dir->i_mutex are locked.
1087 */
1088 di_write_unlock(a->dst_parent);
1089 di_write_lock_parent(a->src_parent);
1090 err = au_wr_dir_need_wh(a->src_dentry,
1091 au_ftest_ren(a->auren_flags,
1092 ISDIR_SRC),
1093 &a->btgt);
1094 di_write_unlock(a->src_parent);
1095 di_write_lock2_parent(a->src_parent, a->dst_parent,
1096 /*isdir*/1);
1097 au_fclr_ren(a->auren_flags, ISSAMEDIR);
1098 } else
1099 err = au_wr_dir_need_wh(a->src_dentry,
1100 au_ftest_ren(a->auren_flags,
1101 ISDIR_SRC),
1102 &a->btgt);
1103 }
1104 if (unlikely(err < 0))
1105 goto out_children;
1106 if (err)
1107 au_fset_ren(a->auren_flags, WHSRC);
1108
1109 /* cpup src */
1110 if (a->src_btop != a->btgt) {
1111 err = au_pin(&pin, a->src_dentry, a->btgt,
1112 au_opt_udba(a->src_dentry->d_sb),
1113 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
1114 if (!err) {
1115 struct au_cp_generic cpg = {
1116 .dentry = a->src_dentry,
1117 .bdst = a->btgt,
1118 .bsrc = a->src_btop,
1119 .len = -1,
1120 .pin = &pin,
1121 .flags = AuCpup_DTIME | AuCpup_HOPEN
1122 };
1123 AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
1124 err = au_sio_cpup_simple(&cpg);
1125 au_unpin(&pin);
1126 }
1127 if (unlikely(err))
1128 goto out_children;
1129 a->src_btop = a->btgt;
1130 a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
1131 if (!a->exchange)
1132 au_fset_ren(a->auren_flags, WHSRC);
1133 }
1134
1135 /* cpup dst */
1136 if (a->exchange && a->dst_inode
1137 && a->dst_btop != a->btgt) {
1138 err = au_pin(&pin, a->dst_dentry, a->btgt,
1139 au_opt_udba(a->dst_dentry->d_sb),
1140 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
1141 if (!err) {
1142 struct au_cp_generic cpg = {
1143 .dentry = a->dst_dentry,
1144 .bdst = a->btgt,
1145 .bsrc = a->dst_btop,
1146 .len = -1,
1147 .pin = &pin,
1148 .flags = AuCpup_DTIME | AuCpup_HOPEN
1149 };
1150 err = au_sio_cpup_simple(&cpg);
1151 au_unpin(&pin);
1152 }
1153 if (unlikely(err))
1154 goto out_children;
1155 a->dst_btop = a->btgt;
1156 a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
1157 }
1158
1159 /* lock them all */
1160 err = au_ren_lock(a);
1161 if (unlikely(err))
1162 /* leave the copied-up one */
1163 goto out_children;
1164
1165 if (!a->exchange) {
1166 if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
1167 err = au_may_ren(a);
1168 else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
1169 err = -ENAMETOOLONG;
1170 if (unlikely(err))
1171 goto out_hdir;
1172 }
1173
1174 /* store timestamps to be revertible */
1175 au_ren_dt(a);
1176
1177 /* store dirren info */
1178 if (au_ftest_ren(a->auren_flags, DIRREN)) {
1179 err = au_dr_rename(a->src_dentry, a->btgt,
1180 &a->dst_dentry->d_name, &rev);
1181 AuTraceErr(err);
1182 if (unlikely(err))
1183 goto out_dt;
1184 }
1185
1186 /* here we go */
1187 err = do_rename(a);
1188 if (unlikely(err))
1189 goto out_dirren;
1190
1191 if (au_ftest_ren(a->auren_flags, DIRREN))
1192 au_dr_rename_fin(a->src_dentry, a->btgt, rev);
1193
1194 /* update dir attributes */
1195 au_ren_refresh_dir(a);
1196
1197 /* dput/iput all lower dentries */
1198 au_ren_refresh(a);
1199
1200 goto out_hdir; /* success */
1201
1202out_dirren:
1203 if (au_ftest_ren(a->auren_flags, DIRREN))
1204 au_dr_rename_rev(a->src_dentry, a->btgt, rev);
1205out_dt:
1206 au_ren_rev_dt(err, a);
1207out_hdir:
1208 au_ren_unlock(a);
1209out_children:
1210 au_nhash_wh_free(&a->whlist);
1211 if (err && a->dst_inode && a->dst_btop != a->btgt) {
1212 AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
1213 au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
1214 au_set_dbtop(a->dst_dentry, a->dst_btop);
1215 }
1216out_parent:
1217 if (!err) {
1218 if (d_unhashed(a->src_dentry))
1219 au_fset_ren(a->auren_flags, DROPPED_SRC);
1220 if (d_unhashed(a->dst_dentry))
1221 au_fset_ren(a->auren_flags, DROPPED_DST);
1222 if (!a->exchange)
1223 d_move(a->src_dentry, a->dst_dentry);
1224 else {
1225 d_exchange(a->src_dentry, a->dst_dentry);
1226 if (au_ftest_ren(a->auren_flags, DROPPED_DST))
1227 d_drop(a->dst_dentry);
1228 }
1229 if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
1230 d_drop(a->src_dentry);
1231 } else {
1232 au_update_dbtop(a->dst_dentry);
1233 if (!a->dst_inode)
1234 d_drop(a->dst_dentry);
1235 }
1236 if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
1237 di_write_unlock(a->dst_parent);
1238 else
1239 di_write_unlock2(a->src_parent, a->dst_parent);
1240out_unlock:
1241 aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
1242out_free:
1243 iput(a->dst_inode);
1244 if (a->thargs)
1245 au_whtmp_rmdir_free(a->thargs);
1246 au_kfree_rcu(a);
1247out:
1248 AuTraceErr(err);
1249 return err;
1250}