2019-05-28 09:57:16 -07:00
// SPDX-License-Identifier: GPL-2.0-only
2013-08-21 17:29:54 -04:00
/*
* Ceph cache definitions .
*
* Copyright ( C ) 2013 by Adfin Solutions , Inc . All Rights Reserved .
* Written by Milosz Tanski ( milosz @ adfin . com )
*/
2019-09-05 17:29:29 +02:00
# include <linux/ceph/ceph_debug.h>
2019-03-25 16:38:32 +00:00
# include <linux/fs_context.h>
2013-08-21 17:29:54 -04:00
# include "super.h"
# include "cache.h"
2021-12-07 08:44:50 -05:00
void ceph_fscache_register_inode_cookie ( struct inode * inode )
2013-08-21 17:29:54 -04:00
{
2021-12-07 08:44:50 -05:00
struct ceph_inode_info * ci = ceph_inode ( inode ) ;
struct ceph_fs_client * fsc = ceph_inode_to_client ( inode ) ;
2017-06-27 11:57:56 +08:00
2021-12-07 08:44:50 -05:00
/* No caching for filesystem? */
if ( ! fsc - > fscache )
return ;
2017-06-27 11:57:56 +08:00
2021-12-07 08:44:50 -05:00
/* Regular files only */
if ( ! S_ISREG ( inode - > i_mode ) )
return ;
2017-06-27 11:57:56 +08:00
2021-12-07 08:44:50 -05:00
/* Only new inodes! */
if ( ! ( inode - > i_state & I_NEW ) )
return ;
2018-04-04 13:41:28 +01:00
2021-06-29 22:37:05 +01:00
WARN_ON_ONCE ( ci - > netfs_ctx . cache ) ;
2013-08-21 17:29:54 -04:00
2021-06-29 22:37:05 +01:00
ci - > netfs_ctx . cache =
fscache_acquire_cookie ( fsc - > fscache , 0 ,
& ci - > i_vino , sizeof ( ci - > i_vino ) ,
& ci - > i_version , sizeof ( ci - > i_version ) ,
i_size_read ( inode ) ) ;
2013-08-21 17:29:54 -04:00
}
2021-06-29 22:37:05 +01:00
void ceph_fscache_unregister_inode_cookie ( struct ceph_inode_info * ci )
2013-08-21 17:29:54 -04:00
{
2021-06-29 22:37:05 +01:00
fscache_relinquish_cookie ( ceph_fscache_cookie ( ci ) , false ) ;
2021-12-07 08:44:50 -05:00
}
2013-08-21 17:29:54 -04:00
2021-12-07 08:44:50 -05:00
void ceph_fscache_use_cookie ( struct inode * inode , bool will_modify )
{
struct ceph_inode_info * ci = ceph_inode ( inode ) ;
2013-08-21 17:29:54 -04:00
2021-06-29 22:37:05 +01:00
fscache_use_cookie ( ceph_fscache_cookie ( ci ) , will_modify ) ;
2013-08-21 17:29:54 -04:00
}
2021-12-07 08:44:50 -05:00
void ceph_fscache_unuse_cookie ( struct inode * inode , bool update )
2013-08-21 17:29:54 -04:00
{
2016-05-18 15:25:03 +08:00
struct ceph_inode_info * ci = ceph_inode ( inode ) ;
2013-08-21 17:29:54 -04:00
2021-12-07 08:44:50 -05:00
if ( update ) {
loff_t i_size = i_size_read ( inode ) ;
2013-08-21 17:29:54 -04:00
2021-06-29 22:37:05 +01:00
fscache_unuse_cookie ( ceph_fscache_cookie ( ci ) ,
& ci - > i_version , & i_size ) ;
2021-12-07 08:44:50 -05:00
} else {
2021-06-29 22:37:05 +01:00
fscache_unuse_cookie ( ceph_fscache_cookie ( ci ) , NULL , NULL ) ;
2016-05-18 15:25:03 +08:00
}
2013-08-21 17:29:54 -04:00
}
2021-12-07 08:44:50 -05:00
void ceph_fscache_update ( struct inode * inode )
2013-08-21 17:29:54 -04:00
{
2021-12-07 08:44:50 -05:00
struct ceph_inode_info * ci = ceph_inode ( inode ) ;
loff_t i_size = i_size_read ( inode ) ;
2013-08-21 17:29:54 -04:00
2021-06-29 22:37:05 +01:00
fscache_update_cookie ( ceph_fscache_cookie ( ci ) , & ci - > i_version , & i_size ) ;
2013-08-21 17:29:54 -04:00
}
2021-12-07 08:44:50 -05:00
void ceph_fscache_invalidate ( struct inode * inode , bool dio_write )
2016-05-18 15:25:03 +08:00
{
2021-12-07 08:44:50 -05:00
struct ceph_inode_info * ci = ceph_inode ( inode ) ;
2021-06-29 22:37:05 +01:00
fscache_invalidate ( ceph_fscache_cookie ( ci ) ,
2021-12-07 08:44:50 -05:00
& ci - > i_version , i_size_read ( inode ) ,
dio_write ? FSCACHE_INVAL_DIO_WRITE : 0 ) ;
2016-05-18 15:25:03 +08:00
}
2021-12-07 08:44:50 -05:00
int ceph_fscache_register_fs ( struct ceph_fs_client * fsc , struct fs_context * fc )
2016-05-18 15:25:03 +08:00
{
2021-12-07 08:44:50 -05:00
const struct ceph_fsid * fsid = & fsc - > client - > fsid ;
const char * fscache_uniq = fsc - > mount_options - > fscache_uniq ;
size_t uniq_len = fscache_uniq ? strlen ( fscache_uniq ) : 0 ;
char * name ;
int err = 0 ;
2016-05-18 15:25:03 +08:00
2021-12-07 08:44:50 -05:00
name = kasprintf ( GFP_KERNEL , " ceph,%pU%s%s " , fsid , uniq_len ? " , " : " " ,
uniq_len ? fscache_uniq : " " ) ;
if ( ! name )
return - ENOMEM ;
2016-05-18 15:25:03 +08:00
2021-12-07 08:44:50 -05:00
fsc - > fscache = fscache_acquire_volume ( name , NULL , NULL , 0 ) ;
if ( IS_ERR_OR_NULL ( fsc - > fscache ) ) {
errorfc ( fc , " Unable to register fscache cookie for %s " , name ) ;
err = fsc - > fscache ? PTR_ERR ( fsc - > fscache ) : - EOPNOTSUPP ;
fsc - > fscache = NULL ;
2016-05-18 15:25:03 +08:00
}
2021-12-07 08:44:50 -05:00
kfree ( name ) ;
return err ;
2016-05-18 15:25:03 +08:00
}
2013-08-21 17:29:54 -04:00
void ceph_fscache_unregister_fs ( struct ceph_fs_client * fsc )
{
2021-12-07 08:44:50 -05:00
fscache_relinquish_volume ( fsc - > fscache , NULL , false ) ;
2013-08-21 17:29:54 -04:00
}