@@ -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+
18871960static 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
19282002static const struct inode_operations fuse_symlink_inode_operations = {
0 commit comments