internal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 ([email protected])
  6. */
  7. #ifdef pr_fmt
  8. #undef pr_fmt
  9. #endif
  10. #define pr_fmt(fmt) "CacheFiles: " fmt
  11. #include <linux/fscache-cache.h>
  12. #include <linux/cred.h>
  13. #include <linux/security.h>
  14. #include <linux/xarray.h>
  15. #include <linux/cachefiles.h>
  16. #define CACHEFILES_DIO_BLOCK_SIZE 4096
  17. struct cachefiles_cache;
  18. struct cachefiles_object;
  19. enum cachefiles_content {
  20. /* These values are saved on disk */
  21. CACHEFILES_CONTENT_NO_DATA = 0, /* No content stored */
  22. CACHEFILES_CONTENT_SINGLE = 1, /* Content is monolithic, all is present */
  23. CACHEFILES_CONTENT_ALL = 2, /* Content is all present, no map */
  24. CACHEFILES_CONTENT_BACKFS_MAP = 3, /* Content is piecemeal, mapped through backing fs */
  25. CACHEFILES_CONTENT_DIRTY = 4, /* Content is dirty (only seen on disk) */
  26. nr__cachefiles_content
  27. };
  28. /*
  29. * Cached volume representation.
  30. */
  31. struct cachefiles_volume {
  32. struct cachefiles_cache *cache;
  33. struct list_head cache_link; /* Link in cache->volumes */
  34. struct fscache_volume *vcookie; /* The netfs's representation */
  35. struct dentry *dentry; /* The volume dentry */
  36. struct dentry *fanout[256]; /* Fanout subdirs */
  37. };
  38. /*
  39. * Backing file state.
  40. */
  41. struct cachefiles_object {
  42. struct fscache_cookie *cookie; /* Netfs data storage object cookie */
  43. struct cachefiles_volume *volume; /* Cache volume that holds this object */
  44. struct list_head cache_link; /* Link in cache->*_list */
  45. struct file *file; /* The file representing this object */
  46. char *d_name; /* Backing file name */
  47. int debug_id;
  48. spinlock_t lock;
  49. refcount_t ref;
  50. u8 d_name_len; /* Length of filename */
  51. enum cachefiles_content content_info:8; /* Info about content presence */
  52. unsigned long flags;
  53. #define CACHEFILES_OBJECT_USING_TMPFILE 0 /* Have an unlinked tmpfile */
  54. #ifdef CONFIG_CACHEFILES_ONDEMAND
  55. int ondemand_id;
  56. #endif
  57. };
  58. #define CACHEFILES_ONDEMAND_ID_CLOSED -1
  59. /*
  60. * Cache files cache definition
  61. */
  62. struct cachefiles_cache {
  63. struct fscache_cache *cache; /* Cache cookie */
  64. struct vfsmount *mnt; /* mountpoint holding the cache */
  65. struct dentry *store; /* Directory into which live objects go */
  66. struct dentry *graveyard; /* directory into which dead objects go */
  67. struct file *cachefilesd; /* manager daemon handle */
  68. struct list_head volumes; /* List of volume objects */
  69. struct list_head object_list; /* List of active objects */
  70. spinlock_t object_list_lock; /* Lock for volumes and object_list */
  71. const struct cred *cache_cred; /* security override for accessing cache */
  72. struct mutex daemon_mutex; /* command serialisation mutex */
  73. wait_queue_head_t daemon_pollwq; /* poll waitqueue for daemon */
  74. atomic_t gravecounter; /* graveyard uniquifier */
  75. atomic_t f_released; /* number of objects released lately */
  76. atomic_long_t b_released; /* number of blocks released lately */
  77. atomic_long_t b_writing; /* Number of blocks being written */
  78. unsigned frun_percent; /* when to stop culling (% files) */
  79. unsigned fcull_percent; /* when to start culling (% files) */
  80. unsigned fstop_percent; /* when to stop allocating (% files) */
  81. unsigned brun_percent; /* when to stop culling (% blocks) */
  82. unsigned bcull_percent; /* when to start culling (% blocks) */
  83. unsigned bstop_percent; /* when to stop allocating (% blocks) */
  84. unsigned bsize; /* cache's block size */
  85. unsigned bshift; /* ilog2(bsize) */
  86. uint64_t frun; /* when to stop culling */
  87. uint64_t fcull; /* when to start culling */
  88. uint64_t fstop; /* when to stop allocating */
  89. sector_t brun; /* when to stop culling */
  90. sector_t bcull; /* when to start culling */
  91. sector_t bstop; /* when to stop allocating */
  92. unsigned long flags;
  93. #define CACHEFILES_READY 0 /* T if cache prepared */
  94. #define CACHEFILES_DEAD 1 /* T if cache dead */
  95. #define CACHEFILES_CULLING 2 /* T if cull engaged */
  96. #define CACHEFILES_STATE_CHANGED 3 /* T if state changed (poll trigger) */
  97. #define CACHEFILES_ONDEMAND_MODE 4 /* T if in on-demand read mode */
  98. char *rootdirname; /* name of cache root directory */
  99. char *secctx; /* LSM security context */
  100. char *tag; /* cache binding tag */
  101. refcount_t unbind_pincount;/* refcount to do daemon unbind */
  102. struct xarray reqs; /* xarray of pending on-demand requests */
  103. unsigned long req_id_next;
  104. struct xarray ondemand_ids; /* xarray for ondemand_id allocation */
  105. u32 ondemand_id_next;
  106. };
  107. static inline bool cachefiles_in_ondemand_mode(struct cachefiles_cache *cache)
  108. {
  109. return IS_ENABLED(CONFIG_CACHEFILES_ONDEMAND) &&
  110. test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags);
  111. }
  112. struct cachefiles_req {
  113. struct cachefiles_object *object;
  114. struct completion done;
  115. int error;
  116. struct cachefiles_msg msg;
  117. };
  118. #define CACHEFILES_REQ_NEW XA_MARK_1
  119. #include <trace/events/cachefiles.h>
  120. static inline
  121. struct file *cachefiles_cres_file(struct netfs_cache_resources *cres)
  122. {
  123. return cres->cache_priv2;
  124. }
  125. static inline
  126. struct cachefiles_object *cachefiles_cres_object(struct netfs_cache_resources *cres)
  127. {
  128. return fscache_cres_cookie(cres)->cache_priv;
  129. }
  130. /*
  131. * note change of state for daemon
  132. */
  133. static inline void cachefiles_state_changed(struct cachefiles_cache *cache)
  134. {
  135. set_bit(CACHEFILES_STATE_CHANGED, &cache->flags);
  136. wake_up_all(&cache->daemon_pollwq);
  137. }
  138. /*
  139. * cache.c
  140. */
  141. extern int cachefiles_add_cache(struct cachefiles_cache *cache);
  142. extern void cachefiles_withdraw_cache(struct cachefiles_cache *cache);
  143. enum cachefiles_has_space_for {
  144. cachefiles_has_space_check,
  145. cachefiles_has_space_for_write,
  146. cachefiles_has_space_for_create,
  147. };
  148. extern int cachefiles_has_space(struct cachefiles_cache *cache,
  149. unsigned fnr, unsigned bnr,
  150. enum cachefiles_has_space_for reason);
  151. /*
  152. * daemon.c
  153. */
  154. extern const struct file_operations cachefiles_daemon_fops;
  155. extern void cachefiles_get_unbind_pincount(struct cachefiles_cache *cache);
  156. extern void cachefiles_put_unbind_pincount(struct cachefiles_cache *cache);
  157. /*
  158. * error_inject.c
  159. */
  160. #ifdef CONFIG_CACHEFILES_ERROR_INJECTION
  161. extern unsigned int cachefiles_error_injection_state;
  162. extern int cachefiles_register_error_injection(void);
  163. extern void cachefiles_unregister_error_injection(void);
  164. #else
  165. #define cachefiles_error_injection_state 0
  166. static inline int cachefiles_register_error_injection(void)
  167. {
  168. return 0;
  169. }
  170. static inline void cachefiles_unregister_error_injection(void)
  171. {
  172. }
  173. #endif
  174. static inline int cachefiles_inject_read_error(void)
  175. {
  176. return cachefiles_error_injection_state & 2 ? -EIO : 0;
  177. }
  178. static inline int cachefiles_inject_write_error(void)
  179. {
  180. return cachefiles_error_injection_state & 2 ? -EIO :
  181. cachefiles_error_injection_state & 1 ? -ENOSPC :
  182. 0;
  183. }
  184. static inline int cachefiles_inject_remove_error(void)
  185. {
  186. return cachefiles_error_injection_state & 2 ? -EIO : 0;
  187. }
  188. /*
  189. * interface.c
  190. */
  191. extern const struct fscache_cache_ops cachefiles_cache_ops;
  192. extern void cachefiles_see_object(struct cachefiles_object *object,
  193. enum cachefiles_obj_ref_trace why);
  194. extern struct cachefiles_object *cachefiles_grab_object(struct cachefiles_object *object,
  195. enum cachefiles_obj_ref_trace why);
  196. extern void cachefiles_put_object(struct cachefiles_object *object,
  197. enum cachefiles_obj_ref_trace why);
  198. /*
  199. * io.c
  200. */
  201. extern bool cachefiles_begin_operation(struct netfs_cache_resources *cres,
  202. enum fscache_want_state want_state);
  203. extern int __cachefiles_prepare_write(struct cachefiles_object *object,
  204. struct file *file,
  205. loff_t *_start, size_t *_len,
  206. bool no_space_allocated_yet);
  207. extern int __cachefiles_write(struct cachefiles_object *object,
  208. struct file *file,
  209. loff_t start_pos,
  210. struct iov_iter *iter,
  211. netfs_io_terminated_t term_func,
  212. void *term_func_priv);
  213. /*
  214. * key.c
  215. */
  216. extern bool cachefiles_cook_key(struct cachefiles_object *object);
  217. /*
  218. * main.c
  219. */
  220. extern struct kmem_cache *cachefiles_object_jar;
  221. /*
  222. * namei.c
  223. */
  224. extern void cachefiles_unmark_inode_in_use(struct cachefiles_object *object,
  225. struct file *file);
  226. extern int cachefiles_bury_object(struct cachefiles_cache *cache,
  227. struct cachefiles_object *object,
  228. struct dentry *dir,
  229. struct dentry *rep,
  230. enum fscache_why_object_killed why);
  231. extern int cachefiles_delete_object(struct cachefiles_object *object,
  232. enum fscache_why_object_killed why);
  233. extern bool cachefiles_look_up_object(struct cachefiles_object *object);
  234. extern struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache,
  235. struct dentry *dir,
  236. const char *name,
  237. bool *_is_new);
  238. extern void cachefiles_put_directory(struct dentry *dir);
  239. extern int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir,
  240. char *filename);
  241. extern int cachefiles_check_in_use(struct cachefiles_cache *cache,
  242. struct dentry *dir, char *filename);
  243. extern struct file *cachefiles_create_tmpfile(struct cachefiles_object *object);
  244. extern bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache,
  245. struct cachefiles_object *object);
  246. /*
  247. * ondemand.c
  248. */
  249. #ifdef CONFIG_CACHEFILES_ONDEMAND
  250. extern ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
  251. char __user *_buffer, size_t buflen);
  252. extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache,
  253. char *args);
  254. extern int cachefiles_ondemand_init_object(struct cachefiles_object *object);
  255. extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object);
  256. extern int cachefiles_ondemand_read(struct cachefiles_object *object,
  257. loff_t pos, size_t len);
  258. #else
  259. static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache,
  260. char __user *_buffer, size_t buflen)
  261. {
  262. return -EOPNOTSUPP;
  263. }
  264. static inline int cachefiles_ondemand_init_object(struct cachefiles_object *object)
  265. {
  266. return 0;
  267. }
  268. static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object)
  269. {
  270. }
  271. static inline int cachefiles_ondemand_read(struct cachefiles_object *object,
  272. loff_t pos, size_t len)
  273. {
  274. return -EOPNOTSUPP;
  275. }
  276. #endif
  277. /*
  278. * security.c
  279. */
  280. extern int cachefiles_get_security_ID(struct cachefiles_cache *cache);
  281. extern int cachefiles_determine_cache_security(struct cachefiles_cache *cache,
  282. struct dentry *root,
  283. const struct cred **_saved_cred);
  284. static inline void cachefiles_begin_secure(struct cachefiles_cache *cache,
  285. const struct cred **_saved_cred)
  286. {
  287. *_saved_cred = override_creds(cache->cache_cred);
  288. }
  289. static inline void cachefiles_end_secure(struct cachefiles_cache *cache,
  290. const struct cred *saved_cred)
  291. {
  292. revert_creds(saved_cred);
  293. }
  294. /*
  295. * volume.c
  296. */
  297. void cachefiles_acquire_volume(struct fscache_volume *volume);
  298. void cachefiles_free_volume(struct fscache_volume *volume);
  299. void cachefiles_withdraw_volume(struct cachefiles_volume *volume);
  300. /*
  301. * xattr.c
  302. */
  303. extern int cachefiles_set_object_xattr(struct cachefiles_object *object);
  304. extern int cachefiles_check_auxdata(struct cachefiles_object *object,
  305. struct file *file);
  306. extern int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
  307. struct cachefiles_object *object,
  308. struct dentry *dentry);
  309. extern void cachefiles_prepare_to_write(struct fscache_cookie *cookie);
  310. extern bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume);
  311. extern int cachefiles_check_volume_xattr(struct cachefiles_volume *volume);
  312. /*
  313. * Error handling
  314. */
  315. #define cachefiles_io_error(___cache, FMT, ...) \
  316. do { \
  317. pr_err("I/O Error: " FMT"\n", ##__VA_ARGS__); \
  318. fscache_io_error((___cache)->cache); \
  319. set_bit(CACHEFILES_DEAD, &(___cache)->flags); \
  320. } while (0)
  321. #define cachefiles_io_error_obj(object, FMT, ...) \
  322. do { \
  323. struct cachefiles_cache *___cache; \
  324. \
  325. ___cache = (object)->volume->cache; \
  326. cachefiles_io_error(___cache, FMT " [o=%08x]", ##__VA_ARGS__, \
  327. (object)->debug_id); \
  328. } while (0)
  329. /*
  330. * Debug tracing
  331. */
  332. extern unsigned cachefiles_debug;
  333. #define CACHEFILES_DEBUG_KENTER 1
  334. #define CACHEFILES_DEBUG_KLEAVE 2
  335. #define CACHEFILES_DEBUG_KDEBUG 4
  336. #define dbgprintk(FMT, ...) \
  337. printk(KERN_DEBUG "[%-6.6s] "FMT"\n", current->comm, ##__VA_ARGS__)
  338. #define kenter(FMT, ...) dbgprintk("==> %s("FMT")", __func__, ##__VA_ARGS__)
  339. #define kleave(FMT, ...) dbgprintk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
  340. #define kdebug(FMT, ...) dbgprintk(FMT, ##__VA_ARGS__)
  341. #if defined(__KDEBUG)
  342. #define _enter(FMT, ...) kenter(FMT, ##__VA_ARGS__)
  343. #define _leave(FMT, ...) kleave(FMT, ##__VA_ARGS__)
  344. #define _debug(FMT, ...) kdebug(FMT, ##__VA_ARGS__)
  345. #elif defined(CONFIG_CACHEFILES_DEBUG)
  346. #define _enter(FMT, ...) \
  347. do { \
  348. if (cachefiles_debug & CACHEFILES_DEBUG_KENTER) \
  349. kenter(FMT, ##__VA_ARGS__); \
  350. } while (0)
  351. #define _leave(FMT, ...) \
  352. do { \
  353. if (cachefiles_debug & CACHEFILES_DEBUG_KLEAVE) \
  354. kleave(FMT, ##__VA_ARGS__); \
  355. } while (0)
  356. #define _debug(FMT, ...) \
  357. do { \
  358. if (cachefiles_debug & CACHEFILES_DEBUG_KDEBUG) \
  359. kdebug(FMT, ##__VA_ARGS__); \
  360. } while (0)
  361. #else
  362. #define _enter(FMT, ...) no_printk("==> %s("FMT")", __func__, ##__VA_ARGS__)
  363. #define _leave(FMT, ...) no_printk("<== %s()"FMT"", __func__, ##__VA_ARGS__)
  364. #define _debug(FMT, ...) no_printk(FMT, ##__VA_ARGS__)
  365. #endif
  366. #if 1 /* defined(__KDEBUGALL) */
  367. #define ASSERT(X) \
  368. do { \
  369. if (unlikely(!(X))) { \
  370. pr_err("\n"); \
  371. pr_err("Assertion failed\n"); \
  372. BUG(); \
  373. } \
  374. } while (0)
  375. #define ASSERTCMP(X, OP, Y) \
  376. do { \
  377. if (unlikely(!((X) OP (Y)))) { \
  378. pr_err("\n"); \
  379. pr_err("Assertion failed\n"); \
  380. pr_err("%lx " #OP " %lx is false\n", \
  381. (unsigned long)(X), (unsigned long)(Y)); \
  382. BUG(); \
  383. } \
  384. } while (0)
  385. #define ASSERTIF(C, X) \
  386. do { \
  387. if (unlikely((C) && !(X))) { \
  388. pr_err("\n"); \
  389. pr_err("Assertion failed\n"); \
  390. BUG(); \
  391. } \
  392. } while (0)
  393. #define ASSERTIFCMP(C, X, OP, Y) \
  394. do { \
  395. if (unlikely((C) && !((X) OP (Y)))) { \
  396. pr_err("\n"); \
  397. pr_err("Assertion failed\n"); \
  398. pr_err("%lx " #OP " %lx is false\n", \
  399. (unsigned long)(X), (unsigned long)(Y)); \
  400. BUG(); \
  401. } \
  402. } while (0)
  403. #else
  404. #define ASSERT(X) do {} while (0)
  405. #define ASSERTCMP(X, OP, Y) do {} while (0)
  406. #define ASSERTIF(C, X) do {} while (0)
  407. #define ASSERTIFCMP(C, X, OP, Y) do {} while (0)
  408. #endif