Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /* General netfs cache on cache files internal defs
0003  *
0004  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #ifdef pr_fmt
0009 #undef pr_fmt
0010 #endif
0011 
0012 #define pr_fmt(fmt) "CacheFiles: " fmt
0013 
0014 
0015 #include <linux/fscache-cache.h>
0016 #include <linux/cred.h>
0017 #include <linux/security.h>
0018 #include <linux/xarray.h>
0019 #include <linux/cachefiles.h>
0020 
0021 #define CACHEFILES_DIO_BLOCK_SIZE 4096
0022 
0023 struct cachefiles_cache;
0024 struct cachefiles_object;
0025 
0026 enum cachefiles_content {
0027     /* These values are saved on disk */
0028     CACHEFILES_CONTENT_NO_DATA  = 0, /* No content stored */
0029     CACHEFILES_CONTENT_SINGLE   = 1, /* Content is monolithic, all is present */
0030     CACHEFILES_CONTENT_ALL      = 2, /* Content is all present, no map */
0031     CACHEFILES_CONTENT_BACKFS_MAP   = 3, /* Content is piecemeal, mapped through backing fs */
0032     CACHEFILES_CONTENT_DIRTY    = 4, /* Content is dirty (only seen on disk) */
0033     nr__cachefiles_content
0034 };
0035 
0036 /*
0037  * Cached volume representation.
0038  */
0039 struct cachefiles_volume {
0040     struct cachefiles_cache     *cache;
0041     struct list_head        cache_link; /* Link in cache->volumes */
0042     struct fscache_volume       *vcookie;   /* The netfs's representation */
0043     struct dentry           *dentry;    /* The volume dentry */
0044     struct dentry           *fanout[256];   /* Fanout subdirs */
0045 };
0046 
0047 /*
0048  * Backing file state.
0049  */
0050 struct cachefiles_object {
0051     struct fscache_cookie       *cookie;    /* Netfs data storage object cookie */
0052     struct cachefiles_volume    *volume;    /* Cache volume that holds this object */
0053     struct list_head        cache_link; /* Link in cache->*_list */
0054     struct file         *file;      /* The file representing this object */
0055     char                *d_name;    /* Backing file name */
0056     int             debug_id;
0057     spinlock_t          lock;
0058     refcount_t          ref;
0059     u8              d_name_len; /* Length of filename */
0060     enum cachefiles_content     content_info:8; /* Info about content presence */
0061     unsigned long           flags;
0062 #define CACHEFILES_OBJECT_USING_TMPFILE 0       /* Have an unlinked tmpfile */
0063 #ifdef CONFIG_CACHEFILES_ONDEMAND
0064     int             ondemand_id;
0065 #endif
0066 };
0067 
0068 #define CACHEFILES_ONDEMAND_ID_CLOSED   -1
0069 
0070 /*
0071  * Cache files cache definition
0072  */
0073 struct cachefiles_cache {
0074     struct fscache_cache        *cache;     /* Cache cookie */
0075     struct vfsmount         *mnt;       /* mountpoint holding the cache */
0076     struct dentry           *store;     /* Directory into which live objects go */
0077     struct dentry           *graveyard; /* directory into which dead objects go */
0078     struct file         *cachefilesd;   /* manager daemon handle */
0079     struct list_head        volumes;    /* List of volume objects */
0080     struct list_head        object_list;    /* List of active objects */
0081     spinlock_t          object_list_lock; /* Lock for volumes and object_list */
0082     const struct cred       *cache_cred;    /* security override for accessing cache */
0083     struct mutex            daemon_mutex;   /* command serialisation mutex */
0084     wait_queue_head_t       daemon_pollwq;  /* poll waitqueue for daemon */
0085     atomic_t            gravecounter;   /* graveyard uniquifier */
0086     atomic_t            f_released; /* number of objects released lately */
0087     atomic_long_t           b_released; /* number of blocks released lately */
0088     atomic_long_t           b_writing;  /* Number of blocks being written */
0089     unsigned            frun_percent;   /* when to stop culling (% files) */
0090     unsigned            fcull_percent;  /* when to start culling (% files) */
0091     unsigned            fstop_percent;  /* when to stop allocating (% files) */
0092     unsigned            brun_percent;   /* when to stop culling (% blocks) */
0093     unsigned            bcull_percent;  /* when to start culling (% blocks) */
0094     unsigned            bstop_percent;  /* when to stop allocating (% blocks) */
0095     unsigned            bsize;      /* cache's block size */
0096     unsigned            bshift;     /* ilog2(bsize) */
0097     uint64_t            frun;       /* when to stop culling */
0098     uint64_t            fcull;      /* when to start culling */
0099     uint64_t            fstop;      /* when to stop allocating */
0100     sector_t            brun;       /* when to stop culling */
0101     sector_t            bcull;      /* when to start culling */
0102     sector_t            bstop;      /* when to stop allocating */
0103     unsigned long           flags;
0104 #define CACHEFILES_READY        0   /* T if cache prepared */
0105 #define CACHEFILES_DEAD         1   /* T if cache dead */
0106 #define CACHEFILES_CULLING      2   /* T if cull engaged */
0107 #define CACHEFILES_STATE_CHANGED    3   /* T if state changed (poll trigger) */
0108 #define CACHEFILES_ONDEMAND_MODE    4   /* T if in on-demand read mode */
0109     char                *rootdirname;   /* name of cache root directory */
0110     char                *secctx;    /* LSM security context */
0111     char                *tag;       /* cache binding tag */
0112     refcount_t          unbind_pincount;/* refcount to do daemon unbind */
0113     struct xarray           reqs;       /* xarray of pending on-demand requests */
0114     unsigned long           req_id_next;
0115     struct xarray           ondemand_ids;   /* xarray for ondemand_id allocation */
0116     u32             ondemand_id_next;
0117 };
0118 
0119 static inline bool cachefiles_in_ondemand_mode(struct cachefiles_cache *cache)
0120 {
0121     return IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND) &&
0122         test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
0123 }
0124 
0125 struct cachefiles_req {
0126     struct cachefiles_object *object;
0127     struct completion done;
0128     int error;
0129     struct cachefiles_msg msg;
0130 };
0131 
0132 #define CACHEFILES_REQ_NEW  XA_MARK_1
0133 
0134 #include <trace/events/cachefiles.h>
0135 
0136 static inline
0137 struct file *cachefiles_cres_file(struct netfs_cache_resources *cres)
0138 {
0139     return cres->cache_priv2;
0140 }
0141 
0142 static inline
0143 struct cachefiles_object *cachefiles_cres_object(struct netfs_cache_resources *cres)
0144 {
0145     return fscache_cres_cookie(cres)->cache_priv;
0146 }
0147 
0148 /*
0149  * note change of state for daemon
0150  */
0151 static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
0152 {
0153     set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
0154     wake_up_all(&cache->daemon_pollwq);
0155 }
0156 
0157 /*
0158  * cache.c
0159  */
0160 extern int cachefiles_add_cache(struct cachefiles_cache *cache);
0161 extern void cachefiles_withdraw_cache(struct cachefiles_cache *cache);
0162 
0163 enum cachefiles_has_space_for {
0164     cachefiles_has_space_check,
0165     cachefiles_has_space_for_write,
0166     cachefiles_has_space_for_create,
0167 };
0168 extern int cachefiles_has_space(struct cachefiles_cache *cache,
0169                 unsigned fnr, unsigned bnr,
0170                 enum cachefiles_has_space_for reason);
0171 
0172 /*
0173  * daemon.c
0174  */
0175 extern const struct file_operations cachefiles_daemon_fops;
0176 extern void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache);
0177 extern void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache);
0178 
0179 /*
0180  * error_inject.c
0181  */
0182 #ifdef CONFIG_CACHEFILES_ERROR_INJECTION
0183 extern unsigned int cachefiles_error_injection_state;
0184 extern int cachefiles_register_error_injection(void);
0185 extern void cachefiles_unregister_error_injection(void);
0186 
0187 #else
0188 #define cachefiles_error_injection_state 0
0189 
0190 static inline int cachefiles_register_error_injection(void)
0191 {
0192     return 0;
0193 }
0194 
0195 static inline void cachefiles_unregister_error_injection(void)
0196 {
0197 }
0198 #endif
0199 
0200 
0201 static inline int cachefiles_inject_read_error(void)
0202 {
0203     return cachefiles_error_injection_state & 2 ? -EIO : 0;
0204 }
0205 
0206 static inline int cachefiles_inject_write_error(void)
0207 {
0208     return cachefiles_error_injection_state & 2 ? -EIO :
0209         cachefiles_error_injection_state & 1 ? -ENOSPC :
0210         0;
0211 }
0212 
0213 static inline int cachefiles_inject_remove_error(void)
0214 {
0215     return cachefiles_error_injection_state & 2 ? -EIO : 0;
0216 }
0217 
0218 /*
0219  * interface.c
0220  */
0221 extern const struct fscache_cache_ops cachefiles_cache_ops;
0222 extern void cachefiles_see_object(struct cachefiles_object *object,
0223                   enum cachefiles_obj_ref_trace why);
0224 extern struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
0225                             enum cachefiles_obj_ref_trace why);
0226 extern void cachefiles_put_object(struct cachefiles_object *object,
0227                   enum cachefiles_obj_ref_trace why);
0228 
0229 /*
0230  * io.c
0231  */
0232 extern bool cachefiles_begin_operation(struct netfs_cache_resources *cres,
0233                        enum fscache_want_state want_state);
0234 extern int __cachefiles_prepare_write(struct cachefiles_object *object,
0235                       struct file *file,
0236                       loff_t *_start, size_t *_len,
0237                       bool no_space_allocated_yet);
0238 extern int __cachefiles_write(struct cachefiles_object *object,
0239                   struct file *file,
0240                   loff_t start_pos,
0241                   struct iov_iter *iter,
0242                   netfs_io_terminated_t term_func,
0243                   void *term_func_priv);
0244 
0245 /*
0246  * key.c
0247  */
0248 extern bool cachefiles_cook_key(struct cachefiles_object *object);
0249 
0250 /*
0251  * main.c
0252  */
0253 extern struct kmem_cache *cachefiles_object_jar;
0254 
0255 /*
0256  * namei.c
0257  */
0258 extern void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
0259                        struct file *file);
0260 extern int cachefiles_bury_object(struct cachefiles_cache *cache,
0261                   struct cachefiles_object *object,
0262                   struct dentry *dir,
0263                   struct dentry *rep,
0264                   enum fscache_why_object_killed why);
0265 extern int cachefiles_delete_object(struct cachefiles_object *object,
0266                     enum fscache_why_object_killed why);
0267 extern bool cachefiles_look_up_object(struct cachefiles_object *object);
0268 extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
0269                            struct dentry *dir,
0270                            const char *name,
0271                            bool *_is_new);
0272 extern void cachefiles_put_directory(struct dentry *dir);
0273 
0274 extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
0275                char *filename);
0276 
0277 extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
0278                    struct dentry *dir, char *filename);
0279 extern struct file *cachefiles_create_tmpfile(struct cachefiles_object *object);
0280 extern bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache,
0281                       struct cachefiles_object *object);
0282 
0283 /*
0284  * ondemand.c
0285  */
0286 #ifdef CONFIG_CACHEFILES_ONDEMAND
0287 extern ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
0288                     char __user *_buffer, size_t buflen);
0289 
0290 extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache,
0291                      char *args);
0292 
0293 extern int cachefiles_ondemand_init_object(struct cachefiles_object *object);
0294 extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object);
0295 
0296 extern int cachefiles_ondemand_read(struct cachefiles_object *object,
0297                     loff_t pos, size_t len);
0298 
0299 #else
0300 static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
0301                     char __user *_buffer, size_t buflen)
0302 {
0303     return -EOPNOTSUPP;
0304 }
0305 
0306 static inline int cachefiles_ondemand_init_object(struct cachefiles_object *object)
0307 {
0308     return 0;
0309 }
0310 
0311 static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
0312 {
0313 }
0314 
0315 static inline int cachefiles_ondemand_read(struct cachefiles_object *object,
0316                        loff_t pos, size_t len)
0317 {
0318     return -EOPNOTSUPP;
0319 }
0320 #endif
0321 
0322 /*
0323  * security.c
0324  */
0325 extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
0326 extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
0327                            struct dentry *root,
0328                            const struct cred **_saved_cred);
0329 
0330 static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
0331                        const struct cred **_saved_cred)
0332 {
0333     *_saved_cred = override_creds(cache->cache_cred);
0334 }
0335 
0336 static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
0337                      const struct cred *saved_cred)
0338 {
0339     revert_creds(saved_cred);
0340 }
0341 
0342 /*
0343  * volume.c
0344  */
0345 void cachefiles_acquire_volume(struct fscache_volume *volume);
0346 void cachefiles_free_volume(struct fscache_volume *volume);
0347 void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
0348 
0349 /*
0350  * xattr.c
0351  */
0352 extern int cachefiles_set_object_xattr(struct cachefiles_object *object);
0353 extern int cachefiles_check_auxdata(struct cachefiles_object *object,
0354                     struct file *file);
0355 extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
0356                       struct cachefiles_object *object,
0357                       struct dentry *dentry);
0358 extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie);
0359 extern bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume);
0360 extern int cachefiles_check_volume_xattr(struct cachefiles_volume *volume);
0361 
0362 /*
0363  * Error handling
0364  */
0365 #define cachefiles_io_error(___cache, FMT, ...)     \
0366 do {                            \
0367     pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__);   \
0368     fscache_io_error((___cache)->cache);        \
0369     set_bit(CACHEFILES_DEAD, &(___cache)->flags);   \
0370 } while (0)
0371 
0372 #define cachefiles_io_error_obj(object, FMT, ...)           \
0373 do {                                    \
0374     struct cachefiles_cache *___cache;              \
0375                                     \
0376     ___cache = (object)->volume->cache;             \
0377     cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__,   \
0378                 (object)->debug_id);            \
0379 } while (0)
0380 
0381 
0382 /*
0383  * Debug tracing
0384  */
0385 extern unsigned cachefiles_debug;
0386 #define CACHEFILES_DEBUG_KENTER 1
0387 #define CACHEFILES_DEBUG_KLEAVE 2
0388 #define CACHEFILES_DEBUG_KDEBUG 4
0389 
0390 #define dbgprintk(FMT, ...) \
0391     printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
0392 
0393 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
0394 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
0395 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
0396 
0397 
0398 #if defined(__KDEBUG)
0399 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
0400 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
0401 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
0402 
0403 #elif defined(CONFIG_CACHEFILES_DEBUG)
0404 #define _enter(FMT, ...)                \
0405 do {                            \
0406     if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
0407         kenter(FMT, ##__VA_ARGS__);     \
0408 } while (0)
0409 
0410 #define _leave(FMT, ...)                \
0411 do {                            \
0412     if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
0413         kleave(FMT, ##__VA_ARGS__);     \
0414 } while (0)
0415 
0416 #define _debug(FMT, ...)                \
0417 do {                            \
0418     if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
0419         kdebug(FMT, ##__VA_ARGS__);     \
0420 } while (0)
0421 
0422 #else
0423 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
0424 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
0425 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
0426 #endif
0427 
0428 #if 1 /* defined(__KDEBUGALL) */
0429 
0430 #define ASSERT(X)                           \
0431 do {                                    \
0432     if (unlikely(!(X))) {                       \
0433         pr_err("\n");                       \
0434         pr_err("Assertion failed\n");       \
0435         BUG();                          \
0436     }                               \
0437 } while (0)
0438 
0439 #define ASSERTCMP(X, OP, Y)                     \
0440 do {                                    \
0441     if (unlikely(!((X) OP (Y)))) {                  \
0442         pr_err("\n");                       \
0443         pr_err("Assertion failed\n");       \
0444         pr_err("%lx " #OP " %lx is false\n",            \
0445                (unsigned long)(X), (unsigned long)(Y));     \
0446         BUG();                          \
0447     }                               \
0448 } while (0)
0449 
0450 #define ASSERTIF(C, X)                          \
0451 do {                                    \
0452     if (unlikely((C) && !(X))) {                    \
0453         pr_err("\n");                       \
0454         pr_err("Assertion failed\n");       \
0455         BUG();                          \
0456     }                               \
0457 } while (0)
0458 
0459 #define ASSERTIFCMP(C, X, OP, Y)                    \
0460 do {                                    \
0461     if (unlikely((C) && !((X) OP (Y)))) {               \
0462         pr_err("\n");                       \
0463         pr_err("Assertion failed\n");       \
0464         pr_err("%lx " #OP " %lx is false\n",            \
0465                (unsigned long)(X), (unsigned long)(Y));     \
0466         BUG();                          \
0467     }                               \
0468 } while (0)
0469 
0470 #else
0471 
0472 #define ASSERT(X)           do {} while (0)
0473 #define ASSERTCMP(X, OP, Y)     do {} while (0)
0474 #define ASSERTIF(C, X)          do {} while (0)
0475 #define ASSERTIFCMP(C, X, OP, Y)    do {} while (0)
0476 
0477 #endif