@@ -316,27 +316,51 @@ static int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
316
316
return err ;
317
317
}
318
318
319
+ static int ovl_link_up (struct dentry * parent , struct dentry * dentry )
320
+ {
321
+ int err ;
322
+ struct dentry * upper ;
323
+ struct dentry * upperdir = ovl_dentry_upper (parent );
324
+ struct inode * udir = d_inode (upperdir );
325
+
326
+ inode_lock_nested (udir , I_MUTEX_PARENT );
327
+ upper = lookup_one_len (dentry -> d_name .name , upperdir ,
328
+ dentry -> d_name .len );
329
+ err = PTR_ERR (upper );
330
+ if (!IS_ERR (upper )) {
331
+ err = ovl_do_link (ovl_dentry_upper (dentry ), udir , upper , true);
332
+ dput (upper );
333
+
334
+ if (!err )
335
+ ovl_dentry_set_upper_alias (dentry );
336
+ }
337
+ inode_unlock (udir );
338
+
339
+ return err ;
340
+ }
341
+
319
342
struct ovl_copy_up_ctx {
320
343
struct dentry * parent ;
321
344
struct dentry * dentry ;
322
345
struct path lowerpath ;
323
346
struct kstat stat ;
324
347
struct kstat pstat ;
325
348
const char * link ;
326
- struct dentry * upperdir ;
349
+ struct dentry * destdir ;
350
+ struct qstr destname ;
327
351
struct dentry * workdir ;
328
352
bool tmpfile ;
353
+ bool origin ;
329
354
};
330
355
331
356
static int ovl_install_temp (struct ovl_copy_up_ctx * c , struct dentry * temp ,
332
357
struct dentry * * newdentry )
333
358
{
334
359
int err ;
335
360
struct dentry * upper ;
336
- struct inode * udir = d_inode (c -> upperdir );
361
+ struct inode * udir = d_inode (c -> destdir );
337
362
338
- upper = lookup_one_len (c -> dentry -> d_name .name , c -> upperdir ,
339
- c -> dentry -> d_name .len );
363
+ upper = lookup_one_len (c -> destname .name , c -> destdir , c -> destname .len );
340
364
if (IS_ERR (upper ))
341
365
return PTR_ERR (upper );
342
366
@@ -345,11 +369,8 @@ static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
345
369
else
346
370
err = ovl_do_rename (d_inode (c -> workdir ), temp , udir , upper , 0 );
347
371
348
- /* Restore timestamps on parent (best effort) */
349
- if (!err ) {
350
- ovl_set_timestamps (c -> upperdir , & c -> pstat );
372
+ if (!err )
351
373
* newdentry = dget (c -> tmpfile ? upper : temp );
352
- }
353
374
dput (upper );
354
375
355
376
return err ;
@@ -439,7 +460,7 @@ static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
439
460
* Don't set origin when we are breaking the association with a lower
440
461
* hard link.
441
462
*/
442
- if (S_ISDIR ( c -> stat . mode ) || c -> stat . nlink == 1 ) {
463
+ if (c -> origin ) {
443
464
err = ovl_set_origin (c -> dentry , c -> lowerpath .dentry , temp );
444
465
if (err )
445
466
return err ;
@@ -450,7 +471,7 @@ static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
450
471
451
472
static int ovl_copy_up_locked (struct ovl_copy_up_ctx * c )
452
473
{
453
- struct inode * udir = c -> upperdir -> d_inode ;
474
+ struct inode * udir = c -> destdir -> d_inode ;
454
475
struct dentry * newdentry = NULL ;
455
476
struct dentry * temp = NULL ;
456
477
int err ;
@@ -473,7 +494,6 @@ static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
473
494
if (err )
474
495
goto out_cleanup ;
475
496
476
- ovl_dentry_set_upper_alias (c -> dentry );
477
497
ovl_inode_update (d_inode (c -> dentry ), newdentry );
478
498
out :
479
499
dput (temp );
@@ -498,24 +518,57 @@ static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
498
518
{
499
519
int err ;
500
520
struct ovl_fs * ofs = c -> dentry -> d_sb -> s_fs_info ;
521
+ bool indexed = false;
501
522
502
- /* Mark parent "impure" because it may now contain non-pure upper */
503
- err = ovl_set_impure (c -> parent , c -> upperdir );
504
- if (err )
505
- return err ;
523
+ if (ovl_indexdir (c -> dentry -> d_sb ) && !S_ISDIR (c -> stat .mode ) &&
524
+ c -> stat .nlink > 1 )
525
+ indexed = true;
526
+
527
+ if (S_ISDIR (c -> stat .mode ) || c -> stat .nlink == 1 || indexed )
528
+ c -> origin = true;
529
+
530
+ if (indexed ) {
531
+ c -> destdir = ovl_indexdir (c -> dentry -> d_sb );
532
+ err = ovl_get_index_name (c -> lowerpath .dentry , & c -> destname );
533
+ if (err )
534
+ return err ;
535
+ } else {
536
+ /*
537
+ * Mark parent "impure" because it may now contain non-pure
538
+ * upper
539
+ */
540
+ err = ovl_set_impure (c -> parent , c -> destdir );
541
+ if (err )
542
+ return err ;
543
+ }
506
544
507
545
/* Should we copyup with O_TMPFILE or with workdir? */
508
546
if (S_ISREG (c -> stat .mode ) && ofs -> tmpfile ) {
509
547
c -> tmpfile = true;
510
- return ovl_copy_up_locked (c );
548
+ err = ovl_copy_up_locked (c );
549
+ } else {
550
+ err = - EIO ;
551
+ if (lock_rename (c -> workdir , c -> destdir ) != NULL ) {
552
+ pr_err ("overlayfs: failed to lock workdir+upperdir\n" );
553
+ } else {
554
+ err = ovl_copy_up_locked (c );
555
+ unlock_rename (c -> workdir , c -> destdir );
556
+ }
511
557
}
512
558
513
- err = - EIO ;
514
- if (lock_rename (c -> workdir , c -> upperdir ) != NULL ) {
515
- pr_err ("overlayfs: failed to lock workdir+upperdir\n" );
516
- } else {
517
- err = ovl_copy_up_locked (c );
518
- unlock_rename (c -> workdir , c -> upperdir );
559
+ if (indexed ) {
560
+ if (!err )
561
+ ovl_set_flag (OVL_INDEX , d_inode (c -> dentry ));
562
+ kfree (c -> destname .name );
563
+ } else if (!err ) {
564
+ struct inode * udir = d_inode (c -> destdir );
565
+
566
+ /* Restore timestamps on parent (best effort) */
567
+ inode_lock (udir );
568
+ ovl_set_timestamps (c -> destdir , & c -> pstat );
569
+ inode_unlock (udir );
570
+
571
+ ovl_dentry_set_upper_alias (c -> dentry );
519
572
}
520
573
521
574
return err ;
@@ -543,7 +596,8 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
543
596
return err ;
544
597
545
598
ovl_path_upper (parent , & parentpath );
546
- ctx .upperdir = parentpath .dentry ;
599
+ ctx .destdir = parentpath .dentry ;
600
+ ctx .destname = dentry -> d_name ;
547
601
548
602
err = vfs_getattr (& parentpath , & ctx .pstat ,
549
603
STATX_ATIME | STATX_MTIME , AT_STATX_SYNC_AS_STAT );
@@ -567,7 +621,10 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
567
621
if (err > 0 )
568
622
err = 0 ;
569
623
} else {
570
- err = ovl_do_copy_up (& ctx );
624
+ if (!ovl_dentry_upper (dentry ))
625
+ err = ovl_do_copy_up (& ctx );
626
+ if (!err && !ovl_dentry_has_upper_alias (dentry ))
627
+ err = ovl_link_up (parent , dentry );
571
628
ovl_copy_up_end (dentry );
572
629
}
573
630
do_delayed_call (& done );
@@ -583,18 +640,30 @@ int ovl_copy_up_flags(struct dentry *dentry, int flags)
583
640
while (!err ) {
584
641
struct dentry * next ;
585
642
struct dentry * parent ;
586
- enum ovl_path_type type = ovl_path_type (dentry );
587
643
588
- if (OVL_TYPE_UPPER (type ))
644
+ /*
645
+ * Check if copy-up has happened as well as for upper alias (in
646
+ * case of hard links) is there.
647
+ *
648
+ * Both checks are lockless:
649
+ * - false negatives: will recheck under oi->lock
650
+ * - false positives:
651
+ * + ovl_dentry_upper() uses memory barriers to ensure the
652
+ * upper dentry is up-to-date
653
+ * + ovl_dentry_has_upper_alias() relies on locking of
654
+ * upper parent i_rwsem to prevent reordering copy-up
655
+ * with rename.
656
+ */
657
+ if (ovl_dentry_upper (dentry ) &&
658
+ ovl_dentry_has_upper_alias (dentry ))
589
659
break ;
590
660
591
661
next = dget (dentry );
592
662
/* find the topmost dentry not yet copied up */
593
663
for (;;) {
594
664
parent = dget_parent (next );
595
665
596
- type = ovl_path_type (parent );
597
- if (OVL_TYPE_UPPER (type ))
666
+ if (ovl_dentry_upper (parent ))
598
667
break ;
599
668
600
669
dput (next );
0 commit comments