Skip to content

Commit b0aa760

Browse files
Maxim PatlasovMiklos Szeredi
authored andcommitted
fuse: Trust kernel i_mtime only
Let the kernel maintain i_mtime locally: - clear S_NOCMTIME - implement i_op->update_time() - flush mtime on fsync and last close - update i_mtime explicitly on truncate and fallocate Fuse inode flag FUSE_I_MTIME_DIRTY serves as indication that local i_mtime should be flushed to the server eventually. Signed-off-by: Maxim Patlasov <MPatlasov@parallels.com> Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
1 parent 8373200 commit b0aa760

4 files changed

Lines changed: 132 additions & 25 deletions

File tree

‎fs/fuse/dir.c‎

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,11 @@ static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
842842
struct fuse_conn *fc = get_fuse_conn(inode);
843843

844844
/* see the comment in fuse_change_attributes() */
845-
if (fc->writeback_cache && S_ISREG(inode->i_mode))
845+
if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
846846
attr->size = i_size_read(inode);
847+
attr->mtime = inode->i_mtime.tv_sec;
848+
attr->mtimensec = inode->i_mtime.tv_nsec;
849+
}
847850

848851
stat->dev = inode->i_sb->s_dev;
849852
stat->ino = attr->ino;
@@ -1482,12 +1485,16 @@ static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
14821485
FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
14831486
}
14841487

1485-
static bool update_mtime(unsigned ivalid)
1488+
static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
14861489
{
14871490
/* Always update if mtime is explicitly set */
14881491
if (ivalid & ATTR_MTIME_SET)
14891492
return true;
14901493

1494+
/* Or if kernel i_mtime is the official one */
1495+
if (trust_local_mtime)
1496+
return true;
1497+
14911498
/* If it's an open(O_TRUNC) or an ftruncate(), don't update */
14921499
if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
14931500
return false;
@@ -1496,7 +1503,8 @@ static bool update_mtime(unsigned ivalid)
14961503
return true;
14971504
}
14981505

1499-
static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1506+
static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
1507+
bool trust_local_mtime)
15001508
{
15011509
unsigned ivalid = iattr->ia_valid;
15021510

@@ -1515,11 +1523,11 @@ static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
15151523
if (!(ivalid & ATTR_ATIME_SET))
15161524
arg->valid |= FATTR_ATIME_NOW;
15171525
}
1518-
if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1526+
if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_mtime)) {
15191527
arg->valid |= FATTR_MTIME;
15201528
arg->mtime = iattr->ia_mtime.tv_sec;
15211529
arg->mtimensec = iattr->ia_mtime.tv_nsec;
1522-
if (!(ivalid & ATTR_MTIME_SET))
1530+
if (!(ivalid & ATTR_MTIME_SET) && !trust_local_mtime)
15231531
arg->valid |= FATTR_MTIME_NOW;
15241532
}
15251533
}
@@ -1568,6 +1576,63 @@ void fuse_release_nowrite(struct inode *inode)
15681576
spin_unlock(&fc->lock);
15691577
}
15701578

1579+
static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_req *req,
1580+
struct inode *inode,
1581+
struct fuse_setattr_in *inarg_p,
1582+
struct fuse_attr_out *outarg_p)
1583+
{
1584+
req->in.h.opcode = FUSE_SETATTR;
1585+
req->in.h.nodeid = get_node_id(inode);
1586+
req->in.numargs = 1;
1587+
req->in.args[0].size = sizeof(*inarg_p);
1588+
req->in.args[0].value = inarg_p;
1589+
req->out.numargs = 1;
1590+
if (fc->minor < 9)
1591+
req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1592+
else
1593+
req->out.args[0].size = sizeof(*outarg_p);
1594+
req->out.args[0].value = outarg_p;
1595+
}
1596+
1597+
/*
1598+
* Flush inode->i_mtime to the server
1599+
*/
1600+
int fuse_flush_mtime(struct file *file, bool nofail)
1601+
{
1602+
struct inode *inode = file->f_mapping->host;
1603+
struct fuse_inode *fi = get_fuse_inode(inode);
1604+
struct fuse_conn *fc = get_fuse_conn(inode);
1605+
struct fuse_req *req = NULL;
1606+
struct fuse_setattr_in inarg;
1607+
struct fuse_attr_out outarg;
1608+
int err;
1609+
1610+
if (nofail) {
1611+
req = fuse_get_req_nofail_nopages(fc, file);
1612+
} else {
1613+
req = fuse_get_req_nopages(fc);
1614+
if (IS_ERR(req))
1615+
return PTR_ERR(req);
1616+
}
1617+
1618+
memset(&inarg, 0, sizeof(inarg));
1619+
memset(&outarg, 0, sizeof(outarg));
1620+
1621+
inarg.valid |= FATTR_MTIME;
1622+
inarg.mtime = inode->i_mtime.tv_sec;
1623+
inarg.mtimensec = inode->i_mtime.tv_nsec;
1624+
1625+
fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
1626+
fuse_request_send(fc, req);
1627+
err = req->out.h.error;
1628+
fuse_put_request(fc, req);
1629+
1630+
if (!err)
1631+
clear_bit(FUSE_I_MTIME_DIRTY, &fi->state);
1632+
1633+
return err;
1634+
}
1635+
15711636
/*
15721637
* Set attributes, and at the same time refresh them.
15731638
*
@@ -1588,6 +1653,7 @@ int fuse_do_setattr(struct inode *inode, struct iattr *attr,
15881653
bool is_wb = fc->writeback_cache;
15891654
loff_t oldsize;
15901655
int err;
1656+
bool trust_local_mtime = is_wb && S_ISREG(inode->i_mode);
15911657

15921658
if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
15931659
attr->ia_valid |= ATTR_FORCE;
@@ -1616,7 +1682,7 @@ int fuse_do_setattr(struct inode *inode, struct iattr *attr,
16161682

16171683
memset(&inarg, 0, sizeof(inarg));
16181684
memset(&outarg, 0, sizeof(outarg));
1619-
iattr_to_fattr(attr, &inarg);
1685+
iattr_to_fattr(attr, &inarg, trust_local_mtime);
16201686
if (file) {
16211687
struct fuse_file *ff = file->private_data;
16221688
inarg.valid |= FATTR_FH;
@@ -1627,17 +1693,7 @@ int fuse_do_setattr(struct inode *inode, struct iattr *attr,
16271693
inarg.valid |= FATTR_LOCKOWNER;
16281694
inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
16291695
}
1630-
req->in.h.opcode = FUSE_SETATTR;
1631-
req->in.h.nodeid = get_node_id(inode);
1632-
req->in.numargs = 1;
1633-
req->in.args[0].size = sizeof(inarg);
1634-
req->in.args[0].value = &inarg;
1635-
req->out.numargs = 1;
1636-
if (fc->minor < 9)
1637-
req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1638-
else
1639-
req->out.args[0].size = sizeof(outarg);
1640-
req->out.args[0].value = &outarg;
1696+
fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
16411697
fuse_request_send(fc, req);
16421698
err = req->out.h.error;
16431699
fuse_put_request(fc, req);
@@ -1654,6 +1710,12 @@ int fuse_do_setattr(struct inode *inode, struct iattr *attr,
16541710
}
16551711

16561712
spin_lock(&fc->lock);
1713+
/* the kernel maintains i_mtime locally */
1714+
if (trust_local_mtime && (attr->ia_valid & ATTR_MTIME)) {
1715+
inode->i_mtime = attr->ia_mtime;
1716+
clear_bit(FUSE_I_MTIME_DIRTY, &fi->state);
1717+
}
1718+
16571719
fuse_change_attributes_common(inode, &outarg.attr,
16581720
attr_timeout(&outarg));
16591721
oldsize = inode->i_size;
@@ -1884,6 +1946,17 @@ static int fuse_removexattr(struct dentry *entry, const char *name)
18841946
return err;
18851947
}
18861948

1949+
static int fuse_update_time(struct inode *inode, struct timespec *now,
1950+
int flags)
1951+
{
1952+
if (flags & S_MTIME) {
1953+
inode->i_mtime = *now;
1954+
set_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state);
1955+
BUG_ON(!S_ISREG(inode->i_mode));
1956+
}
1957+
return 0;
1958+
}
1959+
18871960
static const struct inode_operations fuse_dir_inode_operations = {
18881961
.lookup = fuse_lookup,
18891962
.mkdir = fuse_mkdir,
@@ -1923,6 +1996,7 @@ static const struct inode_operations fuse_common_inode_operations = {
19231996
.getxattr = fuse_getxattr,
19241997
.listxattr = fuse_listxattr,
19251998
.removexattr = fuse_removexattr,
1999+
.update_time = fuse_update_time,
19262000
};
19272001

19282002
static const struct inode_operations fuse_symlink_inode_operations = {

‎fs/fuse/file.c‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ static int fuse_open(struct inode *inode, struct file *file)
308308

309309
static int fuse_release(struct inode *inode, struct file *file)
310310
{
311+
if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state))
312+
fuse_flush_mtime(file, true);
313+
311314
fuse_release_common(file, FUSE_RELEASE);
312315

313316
/* return value is ignored by VFS */
@@ -475,6 +478,12 @@ int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
475478

476479
fuse_sync_writes(inode);
477480

481+
if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state)) {
482+
int err = fuse_flush_mtime(file, false);
483+
if (err)
484+
goto out;
485+
}
486+
478487
req = fuse_get_req_nopages(fc);
479488
if (IS_ERR(req)) {
480489
err = PTR_ERR(req);
@@ -960,16 +969,21 @@ static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
960969
return req->misc.write.out.size;
961970
}
962971

963-
void fuse_write_update_size(struct inode *inode, loff_t pos)
972+
bool fuse_write_update_size(struct inode *inode, loff_t pos)
964973
{
965974
struct fuse_conn *fc = get_fuse_conn(inode);
966975
struct fuse_inode *fi = get_fuse_inode(inode);
976+
bool ret = false;
967977

968978
spin_lock(&fc->lock);
969979
fi->attr_version = ++fc->attr_version;
970-
if (pos > inode->i_size)
980+
if (pos > inode->i_size) {
971981
i_size_write(inode, pos);
982+
ret = true;
983+
}
972984
spin_unlock(&fc->lock);
985+
986+
return ret;
973987
}
974988

975989
static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
@@ -2877,8 +2891,16 @@ static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
28772891
goto out;
28782892

28792893
/* we could have extended the file */
2880-
if (!(mode & FALLOC_FL_KEEP_SIZE))
2881-
fuse_write_update_size(inode, offset + length);
2894+
if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2895+
bool changed = fuse_write_update_size(inode, offset + length);
2896+
2897+
if (changed && fc->writeback_cache) {
2898+
struct fuse_inode *fi = get_fuse_inode(inode);
2899+
2900+
inode->i_mtime = current_fs_time(inode->i_sb);
2901+
set_bit(FUSE_I_MTIME_DIRTY, &fi->state);
2902+
}
2903+
}
28822904

28832905
if (mode & FALLOC_FL_PUNCH_HOLE)
28842906
truncate_pagecache_range(inode, offset, offset + length - 1);

‎fs/fuse/fuse_i.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ enum {
119119
FUSE_I_INIT_RDPLUS,
120120
/** An operation changing file size is in progress */
121121
FUSE_I_SIZE_UNSTABLE,
122+
/** i_mtime has been updated locally; a flush to userspace needed */
123+
FUSE_I_MTIME_DIRTY,
122124
};
123125

124126
struct fuse_conn;
@@ -876,7 +878,9 @@ long fuse_ioctl_common(struct file *file, unsigned int cmd,
876878
unsigned fuse_file_poll(struct file *file, poll_table *wait);
877879
int fuse_dev_release(struct inode *inode, struct file *file);
878880

879-
void fuse_write_update_size(struct inode *inode, loff_t pos);
881+
bool fuse_write_update_size(struct inode *inode, loff_t pos);
882+
883+
int fuse_flush_mtime(struct file *file, bool nofail);
880884

881885
int fuse_do_setattr(struct inode *inode, struct iattr *attr,
882886
struct file *file);

‎fs/fuse/inode.c‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,11 @@ void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
170170
inode->i_blocks = attr->blocks;
171171
inode->i_atime.tv_sec = attr->atime;
172172
inode->i_atime.tv_nsec = attr->atimensec;
173-
inode->i_mtime.tv_sec = attr->mtime;
174-
inode->i_mtime.tv_nsec = attr->mtimensec;
173+
/* mtime from server may be stale due to local buffered write */
174+
if (!fc->writeback_cache || !S_ISREG(inode->i_mode)) {
175+
inode->i_mtime.tv_sec = attr->mtime;
176+
inode->i_mtime.tv_nsec = attr->mtimensec;
177+
}
175178
inode->i_ctime.tv_sec = attr->ctime;
176179
inode->i_ctime.tv_nsec = attr->ctimensec;
177180

@@ -250,6 +253,8 @@ static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
250253
{
251254
inode->i_mode = attr->mode & S_IFMT;
252255
inode->i_size = attr->size;
256+
inode->i_mtime.tv_sec = attr->mtime;
257+
inode->i_mtime.tv_nsec = attr->mtimensec;
253258
if (S_ISREG(inode->i_mode)) {
254259
fuse_init_common(inode);
255260
fuse_init_file_inode(inode);
@@ -296,7 +301,9 @@ struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
296301
return NULL;
297302

298303
if ((inode->i_state & I_NEW)) {
299-
inode->i_flags |= S_NOATIME|S_NOCMTIME;
304+
inode->i_flags |= S_NOATIME;
305+
if (!fc->writeback_cache || !S_ISREG(inode->i_mode))
306+
inode->i_flags |= S_NOCMTIME;
300307
inode->i_generation = generation;
301308
inode->i_data.backing_dev_info = &fc->bdi;
302309
fuse_init_inode(inode, attr);

0 commit comments

Comments
 (0)