Skip to content

Commit 0c28887

Browse files
rhvgoyalMiklos Szeredi
authored andcommitted
ovl: A new xattr OVL_XATTR_METACOPY for file on upper
Now we will have the capability to have upper inodes which might be only metadata copy up and data is still on lower inode. So add a new xattr OVL_XATTR_METACOPY to distinguish between two cases. Presence of OVL_XATTR_METACOPY reflects that file has been copied up metadata only and and data will be copied up later from lower origin. So this xattr is set when a metadata copy takes place and cleared when data copy takes place. We also use a bit in ovl_inode->flags to cache OVL_UPPERDATA which reflects whether ovl inode has data or not (as opposed to metadata only copy up). If a file is copied up metadata only and later when same file is opened for WRITE, then data copy up takes place. We copy up data, remove METACOPY xattr and then set the UPPERDATA flag in ovl_inode->flags. While all these operations happen with oi->lock held, read side of oi->flags can be lockless. That is another thread on another cpu can check if UPPERDATA flag is set or not. So this gives us an ordering requirement w.r.t UPPERDATA flag. That is, if another cpu sees UPPERDATA flag set, then it should be guaranteed that effects of data copy up and remove xattr operations are also visible. For example. CPU1 CPU2 ovl_open() acquire(oi->lock) ovl_open_maybe_copy_up() ovl_copy_up_data() open_open_need_copy_up() vfs_removexattr() ovl_already_copied_up() ovl_dentry_needs_data_copy_up() ovl_set_flag(OVL_UPPERDATA) ovl_test_flag(OVL_UPPERDATA) release(oi->lock) Say CPU2 is copying up data and in the end sets UPPERDATA flag. But if CPU1 perceives the effects of setting UPPERDATA flag but not the effects of preceding operations (ex. upper that is not fully copied up), it will be a problem. Hence this patch introduces smp_wmb() on setting UPPERDATA flag operation and smp_rmb() on UPPERDATA flag test operation. May be some other lock or barrier is already covering it. But I am not sure what that is and is it obvious enough that we will not break it in future. So hence trying to be safe here and introducing barriers explicitly for UPPERDATA flag/bit. Signed-off-by: Vivek Goyal <vgoyal@redhat.com> Reviewed-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
1 parent 2002df8 commit 0c28887

File tree

4 files changed

+142
-11
lines changed

4 files changed

+142
-11
lines changed

fs/overlayfs/copy_up.c

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
180180
return error;
181181
}
182182

183+
static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
184+
{
185+
struct iattr attr = {
186+
.ia_valid = ATTR_SIZE,
187+
.ia_size = stat->size,
188+
};
189+
190+
return notify_change(upperdentry, &attr, NULL);
191+
}
192+
183193
static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
184194
{
185195
struct iattr attr = {
@@ -520,8 +530,18 @@ static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
520530
return err;
521531
}
522532

533+
if (c->metacopy) {
534+
err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
535+
NULL, 0, -EOPNOTSUPP);
536+
if (err)
537+
return err;
538+
}
539+
523540
inode_lock(temp->d_inode);
524-
err = ovl_set_attr(temp, &c->stat);
541+
if (c->metacopy)
542+
err = ovl_set_size(temp, &c->stat);
543+
if (!err)
544+
err = ovl_set_attr(temp, &c->stat);
525545
inode_unlock(temp->d_inode);
526546

527547
return err;
@@ -559,6 +579,8 @@ static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
559579
if (err)
560580
goto out;
561581

582+
if (!c->metacopy)
583+
ovl_set_upperdata(d_inode(c->dentry));
562584
inode = d_inode(c->dentry);
563585
ovl_inode_update(inode, newdentry);
564586
if (S_ISDIR(inode->i_mode))
@@ -681,6 +703,28 @@ static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
681703
return true;
682704
}
683705

706+
/* Copy up data of an inode which was copied up metadata only in the past. */
707+
static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
708+
{
709+
struct path upperpath;
710+
int err;
711+
712+
ovl_path_upper(c->dentry, &upperpath);
713+
if (WARN_ON(upperpath.dentry == NULL))
714+
return -EIO;
715+
716+
err = ovl_copy_up_data(&c->lowerpath, &upperpath, c->stat.size);
717+
if (err)
718+
return err;
719+
720+
err = vfs_removexattr(upperpath.dentry, OVL_XATTR_METACOPY);
721+
if (err)
722+
return err;
723+
724+
ovl_set_upperdata(d_inode(c->dentry));
725+
return err;
726+
}
727+
684728
static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
685729
int flags)
686730
{
@@ -726,7 +770,7 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
726770
return PTR_ERR(ctx.link);
727771
}
728772

729-
err = ovl_copy_up_start(dentry);
773+
err = ovl_copy_up_start(dentry, flags);
730774
/* err < 0: interrupted, err > 0: raced with another copy-up */
731775
if (unlikely(err)) {
732776
if (err > 0)
@@ -736,6 +780,8 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
736780
err = ovl_do_copy_up(&ctx);
737781
if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
738782
err = ovl_link_up(&ctx);
783+
if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
784+
err = ovl_copy_up_meta_inode_data(&ctx);
739785
ovl_copy_up_end(dentry);
740786
}
741787
do_delayed_call(&done);
@@ -761,7 +807,7 @@ int ovl_copy_up_flags(struct dentry *dentry, int flags)
761807
struct dentry *next;
762808
struct dentry *parent = NULL;
763809

764-
if (ovl_already_copied_up(dentry))
810+
if (ovl_already_copied_up(dentry, flags))
765811
break;
766812

767813
next = dget(dentry);
@@ -789,13 +835,13 @@ int ovl_copy_up_flags(struct dentry *dentry, int flags)
789835
static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
790836
{
791837
/* Copy up of disconnected dentry does not set upper alias */
792-
if (ovl_already_copied_up(dentry))
838+
if (ovl_already_copied_up(dentry, flags))
793839
return false;
794840

795841
if (special_file(d_inode(dentry)->i_mode))
796842
return false;
797843

798-
if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
844+
if (!ovl_open_flags_need_copy_up(flags))
799845
return false;
800846

801847
return true;

fs/overlayfs/overlayfs.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ enum ovl_path_type {
2929
#define OVL_XATTR_IMPURE OVL_XATTR_PREFIX "impure"
3030
#define OVL_XATTR_NLINK OVL_XATTR_PREFIX "nlink"
3131
#define OVL_XATTR_UPPER OVL_XATTR_PREFIX "upper"
32+
#define OVL_XATTR_METACOPY OVL_XATTR_PREFIX "metacopy"
3233

3334
enum ovl_inode_flag {
3435
/* Pure upper dir that may contain non pure upper entries */
3536
OVL_IMPURE,
3637
/* Non-merge dir that may contain whiteout entries */
3738
OVL_WHITEOUTS,
3839
OVL_INDEX,
40+
OVL_UPPERDATA,
3941
};
4042

4143
enum ovl_entry_flag {
@@ -191,6 +193,14 @@ static inline struct dentry *ovl_do_tmpfile(struct dentry *dentry, umode_t mode)
191193
return ret;
192194
}
193195

196+
static inline bool ovl_open_flags_need_copy_up(int flags)
197+
{
198+
if (!flags)
199+
return false;
200+
201+
return ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC));
202+
}
203+
194204
/* util.c */
195205
int ovl_want_write(struct dentry *dentry);
196206
void ovl_drop_write(struct dentry *dentry);
@@ -226,6 +236,10 @@ bool ovl_dentry_is_whiteout(struct dentry *dentry);
226236
void ovl_dentry_set_opaque(struct dentry *dentry);
227237
bool ovl_dentry_has_upper_alias(struct dentry *dentry);
228238
void ovl_dentry_set_upper_alias(struct dentry *dentry);
239+
bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags);
240+
bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags);
241+
bool ovl_has_upperdata(struct inode *inode);
242+
void ovl_set_upperdata(struct inode *inode);
229243
bool ovl_redirect_dir(struct super_block *sb);
230244
const char *ovl_dentry_get_redirect(struct dentry *dentry);
231245
void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect);
@@ -236,9 +250,9 @@ void ovl_dir_modified(struct dentry *dentry, bool impurity);
236250
u64 ovl_dentry_version_get(struct dentry *dentry);
237251
bool ovl_is_whiteout(struct dentry *dentry);
238252
struct file *ovl_path_open(struct path *path, int flags);
239-
int ovl_copy_up_start(struct dentry *dentry);
253+
int ovl_copy_up_start(struct dentry *dentry, int flags);
240254
void ovl_copy_up_end(struct dentry *dentry);
241-
bool ovl_already_copied_up(struct dentry *dentry);
255+
bool ovl_already_copied_up(struct dentry *dentry, int flags);
242256
bool ovl_check_origin_xattr(struct dentry *dentry);
243257
bool ovl_check_dir_xattr(struct dentry *dentry, const char *name);
244258
int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,

fs/overlayfs/super.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,7 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
14851485
/* Root is always merge -> can have whiteouts */
14861486
ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
14871487
ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry);
1488+
ovl_set_upperdata(d_inode(root_dentry));
14881489
ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
14891490
ovl_dentry_lower(root_dentry));
14901491

fs/overlayfs/util.c

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,62 @@ void ovl_dentry_set_upper_alias(struct dentry *dentry)
279279
ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
280280
}
281281

282+
static bool ovl_should_check_upperdata(struct inode *inode)
283+
{
284+
if (!S_ISREG(inode->i_mode))
285+
return false;
286+
287+
if (!ovl_inode_lower(inode))
288+
return false;
289+
290+
return true;
291+
}
292+
293+
bool ovl_has_upperdata(struct inode *inode)
294+
{
295+
if (!ovl_should_check_upperdata(inode))
296+
return true;
297+
298+
if (!ovl_test_flag(OVL_UPPERDATA, inode))
299+
return false;
300+
/*
301+
* Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
302+
* ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
303+
* if setting of OVL_UPPERDATA is visible, then effects of writes
304+
* before that are visible too.
305+
*/
306+
smp_rmb();
307+
return true;
308+
}
309+
310+
void ovl_set_upperdata(struct inode *inode)
311+
{
312+
/*
313+
* Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
314+
* if OVL_UPPERDATA flag is visible, then effects of write operations
315+
* before it are visible as well.
316+
*/
317+
smp_wmb();
318+
ovl_set_flag(OVL_UPPERDATA, inode);
319+
}
320+
321+
/* Caller should hold ovl_inode->lock */
322+
bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
323+
{
324+
if (!ovl_open_flags_need_copy_up(flags))
325+
return false;
326+
327+
return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
328+
}
329+
330+
bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
331+
{
332+
if (!ovl_open_flags_need_copy_up(flags))
333+
return false;
334+
335+
return !ovl_has_upperdata(d_inode(dentry));
336+
}
337+
282338
bool ovl_redirect_dir(struct super_block *sb)
283339
{
284340
struct ovl_fs *ofs = sb->s_fs_info;
@@ -377,7 +433,20 @@ struct file *ovl_path_open(struct path *path, int flags)
377433
return dentry_open(path, flags | O_NOATIME, current_cred());
378434
}
379435

380-
bool ovl_already_copied_up(struct dentry *dentry)
436+
/* Caller should hold ovl_inode->lock */
437+
static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
438+
{
439+
bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
440+
441+
if (ovl_dentry_upper(dentry) &&
442+
(ovl_dentry_has_upper_alias(dentry) || disconnected) &&
443+
!ovl_dentry_needs_data_copy_up_locked(dentry, flags))
444+
return true;
445+
446+
return false;
447+
}
448+
449+
bool ovl_already_copied_up(struct dentry *dentry, int flags)
381450
{
382451
bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
383452

@@ -395,19 +464,20 @@ bool ovl_already_copied_up(struct dentry *dentry)
395464
* with rename.
396465
*/
397466
if (ovl_dentry_upper(dentry) &&
398-
(ovl_dentry_has_upper_alias(dentry) || disconnected))
467+
(ovl_dentry_has_upper_alias(dentry) || disconnected) &&
468+
!ovl_dentry_needs_data_copy_up(dentry, flags))
399469
return true;
400470

401471
return false;
402472
}
403473

404-
int ovl_copy_up_start(struct dentry *dentry)
474+
int ovl_copy_up_start(struct dentry *dentry, int flags)
405475
{
406476
struct ovl_inode *oi = OVL_I(d_inode(dentry));
407477
int err;
408478

409479
err = mutex_lock_interruptible(&oi->lock);
410-
if (!err && ovl_already_copied_up(dentry)) {
480+
if (!err && ovl_already_copied_up_locked(dentry, flags)) {
411481
err = 1; /* Already copied up */
412482
mutex_unlock(&oi->lock);
413483
}

0 commit comments

Comments
 (0)