internal.h 16 KB

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