1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* General netfs cache on cache files internal defs 3 * 4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #ifdef pr_fmt 9 #undef pr_fmt 10 #endif 11 12 #define pr_fmt(fmt) "CacheFiles: " fmt 13 14 15 #include <linux/fscache-cache.h> 16 #include <linux/cred.h> 17 #include <linux/security.h> 18 #include <linux/xarray.h> 19 #include <linux/cachefiles.h> 20 21 #define CACHEFILES_DIO_BLOCK_SIZE 4096 22 23 struct cachefiles_cache; 24 struct cachefiles_object; 25 26 enum cachefiles_content { 27 /* These values are saved on disk */ 28 CACHEFILES_CONTENT_NO_DATA = 0, /* No content stored */ 29 CACHEFILES_CONTENT_SINGLE = 1, /* Content is monolithic, all is present */ 30 CACHEFILES_CONTENT_ALL = 2, /* Content is all present, no map */ 31 CACHEFILES_CONTENT_BACKFS_MAP = 3, /* Content is piecemeal, mapped through backing fs */ 32 CACHEFILES_CONTENT_DIRTY = 4, /* Content is dirty (only seen on disk) */ 33 nr__cachefiles_content 34 }; 35 36 /* 37 * Cached volume representation. 38 */ 39 struct cachefiles_volume { 40 struct cachefiles_cache *cache; 41 struct list_head cache_link; /* Link in cache->volumes */ 42 struct fscache_volume *vcookie; /* The netfs's representation */ 43 struct dentry *dentry; /* The volume dentry */ 44 struct dentry *fanout[256]; /* Fanout subdirs */ 45 }; 46 47 enum cachefiles_object_state { 48 CACHEFILES_ONDEMAND_OBJSTATE_CLOSE, /* Anonymous fd closed by daemon or initial state */ 49 CACHEFILES_ONDEMAND_OBJSTATE_OPEN, /* Anonymous fd associated with object is available */ 50 CACHEFILES_ONDEMAND_OBJSTATE_REOPENING, /* Object that was closed and is being reopened. */ 51 CACHEFILES_ONDEMAND_OBJSTATE_DROPPING, /* Object is being dropped. */ 52 }; 53 54 struct cachefiles_ondemand_info { 55 struct work_struct ondemand_work; 56 int ondemand_id; 57 enum cachefiles_object_state state; 58 struct cachefiles_object *object; 59 spinlock_t lock; 60 }; 61 62 /* 63 * Backing file state. 64 */ 65 struct cachefiles_object { 66 struct fscache_cookie *cookie; /* Netfs data storage object cookie */ 67 struct cachefiles_volume *volume; /* Cache volume that holds this object */ 68 struct list_head cache_link; /* Link in cache->*_list */ 69 struct file *file; /* The file representing this object */ 70 char *d_name; /* Backing file name */ 71 int debug_id; 72 spinlock_t lock; 73 refcount_t ref; 74 u8 d_name_len; /* Length of filename */ 75 enum cachefiles_content content_info:8; /* Info about content presence */ 76 unsigned long flags; 77 #define CACHEFILES_OBJECT_USING_TMPFILE 0 /* Have an unlinked tmpfile */ 78 #ifdef CONFIG_CACHEFILES_ONDEMAND 79 struct cachefiles_ondemand_info *ondemand; 80 #endif 81 }; 82 83 #define CACHEFILES_ONDEMAND_ID_CLOSED -1 84 85 /* 86 * Cache files cache definition 87 */ 88 struct cachefiles_cache { 89 struct fscache_cache *cache; /* Cache cookie */ 90 struct vfsmount *mnt; /* mountpoint holding the cache */ 91 struct dentry *store; /* Directory into which live objects go */ 92 struct dentry *graveyard; /* directory into which dead objects go */ 93 struct file *cachefilesd; /* manager daemon handle */ 94 struct list_head volumes; /* List of volume objects */ 95 struct list_head object_list; /* List of active objects */ 96 spinlock_t object_list_lock; /* Lock for volumes and object_list */ 97 const struct cred *cache_cred; /* security override for accessing cache */ 98 struct mutex daemon_mutex; /* command serialisation mutex */ 99 wait_queue_head_t daemon_pollwq; /* poll waitqueue for daemon */ 100 atomic_t gravecounter; /* graveyard uniquifier */ 101 atomic_t f_released; /* number of objects released lately */ 102 atomic_long_t b_released; /* number of blocks released lately */ 103 atomic_long_t b_writing; /* Number of blocks being written */ 104 unsigned frun_percent; /* when to stop culling (% files) */ 105 unsigned fcull_percent; /* when to start culling (% files) */ 106 unsigned fstop_percent; /* when to stop allocating (% files) */ 107 unsigned brun_percent; /* when to stop culling (% blocks) */ 108 unsigned bcull_percent; /* when to start culling (% blocks) */ 109 unsigned bstop_percent; /* when to stop allocating (% blocks) */ 110 unsigned bsize; /* cache's block size */ 111 unsigned bshift; /* ilog2(bsize) */ 112 uint64_t frun; /* when to stop culling */ 113 uint64_t fcull; /* when to start culling */ 114 uint64_t fstop; /* when to stop allocating */ 115 sector_t brun; /* when to stop culling */ 116 sector_t bcull; /* when to start culling */ 117 sector_t bstop; /* when to stop allocating */ 118 unsigned long flags; 119 #define CACHEFILES_READY 0 /* T if cache prepared */ 120 #define CACHEFILES_DEAD 1 /* T if cache dead */ 121 #define CACHEFILES_CULLING 2 /* T if cull engaged */ 122 #define CACHEFILES_STATE_CHANGED 3 /* T if state changed (poll trigger) */ 123 #define CACHEFILES_ONDEMAND_MODE 4 /* T if in on-demand read mode */ 124 char *rootdirname; /* name of cache root directory */ 125 char *tag; /* cache binding tag */ 126 refcount_t unbind_pincount;/* refcount to do daemon unbind */ 127 struct xarray reqs; /* xarray of pending on-demand requests */ 128 unsigned long req_id_next; 129 struct xarray ondemand_ids; /* xarray for ondemand_id allocation */ 130 u32 ondemand_id_next; 131 u32 msg_id_next; 132 u32 secid; /* LSM security id */ 133 bool have_secid; /* whether "secid" was set */ 134 }; 135 136 static inline bool cachefiles_in_ondemand_mode(struct cachefiles_cache *cache) 137 { 138 return IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND) && 139 test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags); 140 } 141 142 struct cachefiles_req { 143 struct cachefiles_object *object; 144 struct completion done; 145 refcount_t ref; 146 int error; 147 struct cachefiles_msg msg; 148 }; 149 150 #define CACHEFILES_REQ_NEW XA_MARK_1 151 152 #include <trace/events/cachefiles.h> 153 154 static inline 155 struct file *cachefiles_cres_file(struct netfs_cache_resources *cres) 156 { 157 return cres->cache_priv2; 158 } 159 160 static inline 161 struct cachefiles_object *cachefiles_cres_object(struct netfs_cache_resources *cres) 162 { 163 return fscache_cres_cookie(cres)->cache_priv; 164 } 165 166 /* 167 * note change of state for daemon 168 */ 169 static inline void cachefiles_state_changed(struct cachefiles_cache *cache) 170 { 171 set_bit(CACHEFILES_STATE_CHANGED, &cache->flags); 172 wake_up_all(&cache->daemon_pollwq); 173 } 174 175 /* 176 * cache.c 177 */ 178 extern int cachefiles_add_cache(struct cachefiles_cache *cache); 179 extern void cachefiles_withdraw_cache(struct cachefiles_cache *cache); 180 181 enum cachefiles_has_space_for { 182 cachefiles_has_space_check, 183 cachefiles_has_space_for_write, 184 cachefiles_has_space_for_create, 185 }; 186 extern int cachefiles_has_space(struct cachefiles_cache *cache, 187 unsigned fnr, unsigned bnr, 188 enum cachefiles_has_space_for reason); 189 190 /* 191 * daemon.c 192 */ 193 extern const struct file_operations cachefiles_daemon_fops; 194 extern void cachefiles_flush_reqs(struct cachefiles_cache *cache); 195 extern void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache); 196 extern void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache); 197 198 /* 199 * error_inject.c 200 */ 201 #ifdef CONFIG_CACHEFILES_ERROR_INJECTION 202 extern unsigned int cachefiles_error_injection_state; 203 extern int cachefiles_register_error_injection(void); 204 extern void cachefiles_unregister_error_injection(void); 205 206 #else 207 #define cachefiles_error_injection_state 0 208 209 static inline int cachefiles_register_error_injection(void) 210 { 211 return 0; 212 } 213 214 static inline void cachefiles_unregister_error_injection(void) 215 { 216 } 217 #endif 218 219 220 static inline int cachefiles_inject_read_error(void) 221 { 222 return cachefiles_error_injection_state & 2 ? -EIO : 0; 223 } 224 225 static inline int cachefiles_inject_write_error(void) 226 { 227 return cachefiles_error_injection_state & 2 ? -EIO : 228 cachefiles_error_injection_state & 1 ? -ENOSPC : 229 0; 230 } 231 232 static inline int cachefiles_inject_remove_error(void) 233 { 234 return cachefiles_error_injection_state & 2 ? -EIO : 0; 235 } 236 237 /* 238 * interface.c 239 */ 240 extern const struct fscache_cache_ops cachefiles_cache_ops; 241 extern void cachefiles_see_object(struct cachefiles_object *object, 242 enum cachefiles_obj_ref_trace why); 243 extern struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object, 244 enum cachefiles_obj_ref_trace why); 245 extern void cachefiles_put_object(struct cachefiles_object *object, 246 enum cachefiles_obj_ref_trace why); 247 248 /* 249 * io.c 250 */ 251 extern bool cachefiles_begin_operation(struct netfs_cache_resources *cres, 252 enum fscache_want_state want_state); 253 extern int __cachefiles_prepare_write(struct cachefiles_object *object, 254 struct file *file, 255 loff_t *_start, size_t *_len, size_t upper_len, 256 bool no_space_allocated_yet); 257 extern int __cachefiles_write(struct cachefiles_object *object, 258 struct file *file, 259 loff_t start_pos, 260 struct iov_iter *iter, 261 netfs_io_terminated_t term_func, 262 void *term_func_priv); 263 264 /* 265 * key.c 266 */ 267 extern bool cachefiles_cook_key(struct cachefiles_object *object); 268 269 /* 270 * main.c 271 */ 272 extern struct kmem_cache *cachefiles_object_jar; 273 274 /* 275 * namei.c 276 */ 277 extern void cachefiles_unmark_inode_in_use(struct cachefiles_object *object, 278 struct file *file); 279 extern int cachefiles_bury_object(struct cachefiles_cache *cache, 280 struct cachefiles_object *object, 281 struct dentry *dir, 282 struct dentry *rep, 283 enum fscache_why_object_killed why); 284 extern int cachefiles_delete_object(struct cachefiles_object *object, 285 enum fscache_why_object_killed why); 286 extern bool cachefiles_look_up_object(struct cachefiles_object *object); 287 extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache, 288 struct dentry *dir, 289 const char *name, 290 bool *_is_new); 291 extern void cachefiles_put_directory(struct dentry *dir); 292 293 extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir, 294 char *filename); 295 296 extern int cachefiles_check_in_use(struct cachefiles_cache *cache, 297 struct dentry *dir, char *filename); 298 extern struct file *cachefiles_create_tmpfile(struct cachefiles_object *object); 299 extern bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache, 300 struct cachefiles_object *object); 301 302 /* 303 * ondemand.c 304 */ 305 #ifdef CONFIG_CACHEFILES_ONDEMAND 306 extern ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, 307 char __user *_buffer, size_t buflen); 308 309 extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache, 310 char *args); 311 312 extern int cachefiles_ondemand_restore(struct cachefiles_cache *cache, 313 char *args); 314 315 extern int cachefiles_ondemand_init_object(struct cachefiles_object *object); 316 extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object); 317 318 extern int cachefiles_ondemand_read(struct cachefiles_object *object, 319 loff_t pos, size_t len); 320 321 extern int cachefiles_ondemand_init_obj_info(struct cachefiles_object *obj, 322 struct cachefiles_volume *volume); 323 extern void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *obj); 324 325 #define CACHEFILES_OBJECT_STATE_FUNCS(_state, _STATE) \ 326 static inline bool \ 327 cachefiles_ondemand_object_is_##_state(const struct cachefiles_object *object) \ 328 { \ 329 return object->ondemand->state == CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \ 330 } \ 331 \ 332 static inline void \ 333 cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \ 334 { \ 335 object->ondemand->state = CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \ 336 } 337 338 CACHEFILES_OBJECT_STATE_FUNCS(open, OPEN); 339 CACHEFILES_OBJECT_STATE_FUNCS(close, CLOSE); 340 CACHEFILES_OBJECT_STATE_FUNCS(reopening, REOPENING); 341 CACHEFILES_OBJECT_STATE_FUNCS(dropping, DROPPING); 342 343 static inline bool cachefiles_ondemand_is_reopening_read(struct cachefiles_req *req) 344 { 345 return cachefiles_ondemand_object_is_reopening(req->object) && 346 req->msg.opcode == CACHEFILES_OP_READ; 347 } 348 349 #else 350 static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, 351 char __user *_buffer, size_t buflen) 352 { 353 return -EOPNOTSUPP; 354 } 355 356 static inline int cachefiles_ondemand_init_object(struct cachefiles_object *object) 357 { 358 return 0; 359 } 360 361 static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object) 362 { 363 } 364 365 static inline int cachefiles_ondemand_read(struct cachefiles_object *object, 366 loff_t pos, size_t len) 367 { 368 return -EOPNOTSUPP; 369 } 370 371 static inline int cachefiles_ondemand_init_obj_info(struct cachefiles_object *obj, 372 struct cachefiles_volume *volume) 373 { 374 return 0; 375 } 376 static inline void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *obj) 377 { 378 } 379 380 static inline bool cachefiles_ondemand_is_reopening_read(struct cachefiles_req *req) 381 { 382 return false; 383 } 384 #endif 385 386 /* 387 * security.c 388 */ 389 extern int cachefiles_get_security_ID(struct cachefiles_cache *cache); 390 extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache, 391 struct dentry *root, 392 const struct cred **_saved_cred); 393 394 static inline void cachefiles_begin_secure(struct cachefiles_cache *cache, 395 const struct cred **_saved_cred) 396 { 397 *_saved_cred = override_creds(cache->cache_cred); 398 } 399 400 static inline void cachefiles_end_secure(struct cachefiles_cache *cache, 401 const struct cred *saved_cred) 402 { 403 revert_creds(saved_cred); 404 } 405 406 /* 407 * volume.c 408 */ 409 void cachefiles_acquire_volume(struct fscache_volume *volume); 410 void cachefiles_free_volume(struct fscache_volume *volume); 411 void cachefiles_withdraw_volume(struct cachefiles_volume *volume); 412 413 /* 414 * xattr.c 415 */ 416 extern int cachefiles_set_object_xattr(struct cachefiles_object *object); 417 extern int cachefiles_check_auxdata(struct cachefiles_object *object, 418 struct file *file); 419 extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache, 420 struct cachefiles_object *object, 421 struct dentry *dentry); 422 extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie); 423 extern bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume); 424 extern int cachefiles_check_volume_xattr(struct cachefiles_volume *volume); 425 426 /* 427 * Error handling 428 */ 429 #define cachefiles_io_error(___cache, FMT, ...) \ 430 do { \ 431 pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \ 432 fscache_io_error((___cache)->cache); \ 433 set_bit(CACHEFILES_DEAD, &(___cache)->flags); \ 434 if (cachefiles_in_ondemand_mode(___cache)) \ 435 cachefiles_flush_reqs(___cache); \ 436 } while (0) 437 438 #define cachefiles_io_error_obj(object, FMT, ...) \ 439 do { \ 440 struct cachefiles_cache *___cache; \ 441 \ 442 ___cache = (object)->volume->cache; \ 443 cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__, \ 444 (object)->debug_id); \ 445 } while (0) 446 447 448 /* 449 * Debug tracing 450 */ 451 extern unsigned cachefiles_debug; 452 #define CACHEFILES_DEBUG_KENTER 1 453 #define CACHEFILES_DEBUG_KLEAVE 2 454 #define CACHEFILES_DEBUG_KDEBUG 4 455 456 #define dbgprintk(FMT, ...) \ 457 printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__) 458 459 #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__) 460 #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__) 461 #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__) 462 463 464 #if defined(__KDEBUG) 465 #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__) 466 #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__) 467 #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__) 468 469 #elif defined(CONFIG_CACHEFILES_DEBUG) 470 #define _enter(FMT, ...) \ 471 do { \ 472 if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \ 473 kenter(FMT, ##__VA_ARGS__); \ 474 } while (0) 475 476 #define _leave(FMT, ...) \ 477 do { \ 478 if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \ 479 kleave(FMT, ##__VA_ARGS__); \ 480 } while (0) 481 482 #define _debug(FMT, ...) \ 483 do { \ 484 if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \ 485 kdebug(FMT, ##__VA_ARGS__); \ 486 } while (0) 487 488 #else 489 #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__) 490 #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__) 491 #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__) 492 #endif 493 494 #if 1 /* defined(__KDEBUGALL) */ 495 496 #define ASSERT(X) \ 497 do { \ 498 if (unlikely(!(X))) { \ 499 pr_err("\n"); \ 500 pr_err("Assertion failed\n"); \ 501 BUG(); \ 502 } \ 503 } while (0) 504 505 #define ASSERTCMP(X, OP, Y) \ 506 do { \ 507 if (unlikely(!((X) OP (Y)))) { \ 508 pr_err("\n"); \ 509 pr_err("Assertion failed\n"); \ 510 pr_err("%lx " #OP " %lx is false\n", \ 511 (unsigned long)(X), (unsigned long)(Y)); \ 512 BUG(); \ 513 } \ 514 } while (0) 515 516 #define ASSERTIF(C, X) \ 517 do { \ 518 if (unlikely((C) && !(X))) { \ 519 pr_err("\n"); \ 520 pr_err("Assertion failed\n"); \ 521 BUG(); \ 522 } \ 523 } while (0) 524 525 #define ASSERTIFCMP(C, X, OP, Y) \ 526 do { \ 527 if (unlikely((C) && !((X) OP (Y)))) { \ 528 pr_err("\n"); \ 529 pr_err("Assertion failed\n"); \ 530 pr_err("%lx " #OP " %lx is false\n", \ 531 (unsigned long)(X), (unsigned long)(Y)); \ 532 BUG(); \ 533 } \ 534 } while (0) 535 536 #else 537 538 #define ASSERT(X) do {} while (0) 539 #define ASSERTCMP(X, OP, Y) do {} while (0) 540 #define ASSERTIF(C, X) do {} while (0) 541 #define ASSERTIFCMP(C, X, OP, Y) do {} while (0) 542 543 #endif 544