@@ -193,11 +193,11 @@ static int ovl_copy_fileattr(struct inode *inode, struct path *old,
193
193
return ovl_real_fileattr_set (new , & newfa );
194
194
}
195
195
196
- static int ovl_copy_up_data (struct ovl_fs * ofs , struct path * old ,
197
- struct path * new , loff_t len )
196
+ static int ovl_copy_up_file (struct ovl_fs * ofs , struct dentry * dentry ,
197
+ struct file * new_file , loff_t len )
198
198
{
199
+ struct path datapath ;
199
200
struct file * old_file ;
200
- struct file * new_file ;
201
201
loff_t old_pos = 0 ;
202
202
loff_t new_pos = 0 ;
203
203
loff_t cloned ;
@@ -206,23 +206,18 @@ static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old,
206
206
bool skip_hole = false;
207
207
int error = 0 ;
208
208
209
- if (len == 0 )
210
- return 0 ;
209
+ ovl_path_lowerdata (dentry , & datapath );
210
+ if (WARN_ON (datapath .dentry == NULL ))
211
+ return - EIO ;
211
212
212
- old_file = ovl_path_open (old , O_LARGEFILE | O_RDONLY );
213
+ old_file = ovl_path_open (& datapath , O_LARGEFILE | O_RDONLY );
213
214
if (IS_ERR (old_file ))
214
215
return PTR_ERR (old_file );
215
216
216
- new_file = ovl_path_open (new , O_LARGEFILE | O_WRONLY );
217
- if (IS_ERR (new_file )) {
218
- error = PTR_ERR (new_file );
219
- goto out_fput ;
220
- }
221
-
222
217
/* Try to use clone_file_range to clone up within the same fs */
223
218
cloned = do_clone_file_range (old_file , 0 , new_file , 0 , len , 0 );
224
219
if (cloned == len )
225
- goto out ;
220
+ goto out_fput ;
226
221
/* Couldn't clone, so now we try to copy the data */
227
222
228
223
/* Check if lower fs supports seek operation */
@@ -282,10 +277,8 @@ static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old,
282
277
283
278
len -= bytes ;
284
279
}
285
- out :
286
280
if (!error && ovl_should_sync (ofs ))
287
281
error = vfs_fsync (new_file , 0 );
288
- fput (new_file );
289
282
out_fput :
290
283
fput (old_file );
291
284
return error ;
@@ -556,30 +549,31 @@ static int ovl_link_up(struct ovl_copy_up_ctx *c)
556
549
return err ;
557
550
}
558
551
559
- static int ovl_copy_up_inode (struct ovl_copy_up_ctx * c , struct dentry * temp )
552
+ static int ovl_copy_up_data (struct ovl_copy_up_ctx * c , const struct path * temp )
560
553
{
561
554
struct ovl_fs * ofs = OVL_FS (c -> dentry -> d_sb );
562
- struct inode * inode = d_inode (c -> dentry );
563
- struct path upperpath , datapath ;
555
+ struct file * new_file ;
564
556
int err ;
565
557
566
- ovl_path_upper (c -> dentry , & upperpath );
567
- if (WARN_ON (upperpath .dentry != NULL ))
568
- return - EIO ;
558
+ if (!S_ISREG (c -> stat .mode ) || c -> metacopy || !c -> stat .size )
559
+ return 0 ;
569
560
570
- upperpath .dentry = temp ;
561
+ new_file = ovl_path_open (temp , O_LARGEFILE | O_WRONLY );
562
+ if (IS_ERR (new_file ))
563
+ return PTR_ERR (new_file );
571
564
572
- /*
573
- * Copy up data first and then xattrs. Writing data after
574
- * xattrs will remove security.capability xattr automatically.
575
- */
576
- if (S_ISREG (c -> stat .mode ) && !c -> metacopy ) {
577
- ovl_path_lowerdata (c -> dentry , & datapath );
578
- err = ovl_copy_up_data (ofs , & datapath , & upperpath ,
579
- c -> stat .size );
580
- if (err )
581
- return err ;
582
- }
565
+ err = ovl_copy_up_file (ofs , c -> dentry , new_file , c -> stat .size );
566
+ fput (new_file );
567
+
568
+ return err ;
569
+ }
570
+
571
+ static int ovl_copy_up_metadata (struct ovl_copy_up_ctx * c , struct dentry * temp )
572
+ {
573
+ struct ovl_fs * ofs = OVL_FS (c -> dentry -> d_sb );
574
+ struct inode * inode = d_inode (c -> dentry );
575
+ struct path upperpath = { .mnt = ovl_upper_mnt (ofs ), .dentry = temp };
576
+ int err ;
583
577
584
578
err = ovl_copy_xattr (c -> dentry -> d_sb , & c -> lowerpath , temp );
585
579
if (err )
@@ -662,6 +656,7 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
662
656
struct ovl_fs * ofs = OVL_FS (c -> dentry -> d_sb );
663
657
struct inode * inode ;
664
658
struct inode * udir = d_inode (c -> destdir ), * wdir = d_inode (c -> workdir );
659
+ struct path path = { .mnt = ovl_upper_mnt (ofs ) };
665
660
struct dentry * temp , * upper ;
666
661
struct ovl_cu_creds cc ;
667
662
int err ;
@@ -688,7 +683,16 @@ static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
688
683
if (IS_ERR (temp ))
689
684
goto unlock ;
690
685
691
- err = ovl_copy_up_inode (c , temp );
686
+ /*
687
+ * Copy up data first and then xattrs. Writing data after
688
+ * xattrs will remove security.capability xattr automatically.
689
+ */
690
+ path .dentry = temp ;
691
+ err = ovl_copy_up_data (c , & path );
692
+ if (err )
693
+ goto cleanup ;
694
+
695
+ err = ovl_copy_up_metadata (c , temp );
692
696
if (err )
693
697
goto cleanup ;
694
698
@@ -732,22 +736,30 @@ static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
732
736
struct ovl_fs * ofs = OVL_FS (c -> dentry -> d_sb );
733
737
struct inode * udir = d_inode (c -> destdir );
734
738
struct dentry * temp , * upper ;
739
+ struct file * tmpfile ;
735
740
struct ovl_cu_creds cc ;
736
741
int err ;
737
742
738
743
err = ovl_prep_cu_creds (c -> dentry , & cc );
739
744
if (err )
740
745
return err ;
741
746
742
- temp = ovl_do_tmpfile (ofs , c -> workdir , c -> stat .mode );
747
+ tmpfile = ovl_do_tmpfile (ofs , c -> workdir , c -> stat .mode );
743
748
ovl_revert_cu_creds (& cc );
744
749
745
- if (IS_ERR (temp ))
746
- return PTR_ERR (temp );
750
+ if (IS_ERR (tmpfile ))
751
+ return PTR_ERR (tmpfile );
747
752
748
- err = ovl_copy_up_inode (c , temp );
753
+ temp = tmpfile -> f_path .dentry ;
754
+ if (!c -> metacopy && c -> stat .size ) {
755
+ err = ovl_copy_up_file (ofs , c -> dentry , tmpfile , c -> stat .size );
756
+ if (err )
757
+ return err ;
758
+ }
759
+
760
+ err = ovl_copy_up_metadata (c , temp );
749
761
if (err )
750
- goto out_dput ;
762
+ goto out_fput ;
751
763
752
764
inode_lock_nested (udir , I_MUTEX_PARENT );
753
765
@@ -761,16 +773,14 @@ static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
761
773
inode_unlock (udir );
762
774
763
775
if (err )
764
- goto out_dput ;
776
+ goto out_fput ;
765
777
766
778
if (!c -> metacopy )
767
779
ovl_set_upperdata (d_inode (c -> dentry ));
768
- ovl_inode_update (d_inode (c -> dentry ), temp );
780
+ ovl_inode_update (d_inode (c -> dentry ), dget ( temp ) );
769
781
770
- return 0 ;
771
-
772
- out_dput :
773
- dput (temp );
782
+ out_fput :
783
+ fput (tmpfile );
774
784
return err ;
775
785
}
776
786
@@ -899,7 +909,7 @@ static ssize_t ovl_getxattr_value(struct path *path, char *name, char **value)
899
909
static int ovl_copy_up_meta_inode_data (struct ovl_copy_up_ctx * c )
900
910
{
901
911
struct ovl_fs * ofs = OVL_FS (c -> dentry -> d_sb );
902
- struct path upperpath , datapath ;
912
+ struct path upperpath ;
903
913
int err ;
904
914
char * capability = NULL ;
905
915
ssize_t cap_size ;
@@ -908,18 +918,14 @@ static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
908
918
if (WARN_ON (upperpath .dentry == NULL ))
909
919
return - EIO ;
910
920
911
- ovl_path_lowerdata (c -> dentry , & datapath );
912
- if (WARN_ON (datapath .dentry == NULL ))
913
- return - EIO ;
914
-
915
921
if (c -> stat .size ) {
916
922
err = cap_size = ovl_getxattr_value (& upperpath , XATTR_NAME_CAPS ,
917
923
& capability );
918
924
if (cap_size < 0 )
919
925
goto out ;
920
926
}
921
927
922
- err = ovl_copy_up_data (ofs , & datapath , & upperpath , c -> stat . size );
928
+ err = ovl_copy_up_data (c , & upperpath );
923
929
if (err )
924
930
goto out_free ;
925
931
0 commit comments