fs/cachefiles/daemon.c
Source file repositories/reference/linux-study-clean/fs/cachefiles/daemon.c
File Facts
- System
- Linux kernel
- Corpus path
fs/cachefiles/daemon.c- Extension
.c- Size
- 19012 bytes
- Lines
- 828
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/module.hlinux/init.hlinux/sched.hlinux/completion.hlinux/slab.hlinux/fs.hlinux/file.hlinux/namei.hlinux/poll.hlinux/mount.hlinux/security.hlinux/statfs.hlinux/ctype.hlinux/string.hlinux/fs_struct.hinternal.h
Detected Declarations
struct cachefiles_daemon_cmdfunction cachefiles_daemon_openfunction cachefiles_flush_reqsfunction cachefiles_put_unbind_pincountfunction cachefiles_get_unbind_pincountfunction cachefiles_daemon_releasefunction cachefiles_do_daemon_readfunction cachefiles_daemon_readfunction cachefiles_daemon_writefunction cachefiles_daemon_pollfunction xas_for_each_markedfunction cachefiles_daemon_range_errorfunction cachefiles_daemon_frunfunction cachefiles_daemon_fcullfunction cachefiles_daemon_fstopfunction cachefiles_daemon_brunfunction cachefiles_daemon_bcullfunction cachefiles_daemon_bstopfunction cachefiles_daemon_dirfunction cachefiles_daemon_secctxfunction cachefiles_daemon_tagfunction cachefiles_daemon_cullfunction cachefiles_daemon_debugfunction cachefiles_daemon_inusefunction cachefiles_daemon_bindfunction cachefiles_daemon_unbind
Annotated Snippet
const struct file_operations cachefiles_daemon_fops = {
.owner = THIS_MODULE,
.open = cachefiles_daemon_open,
.release = cachefiles_daemon_release,
.read = cachefiles_daemon_read,
.write = cachefiles_daemon_write,
.poll = cachefiles_daemon_poll,
.llseek = noop_llseek,
};
struct cachefiles_daemon_cmd {
char name[8];
int (*handler)(struct cachefiles_cache *cache, char *args);
};
static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = {
{ "bind", cachefiles_daemon_bind },
{ "brun", cachefiles_daemon_brun },
{ "bcull", cachefiles_daemon_bcull },
{ "bstop", cachefiles_daemon_bstop },
{ "cull", cachefiles_daemon_cull },
{ "debug", cachefiles_daemon_debug },
{ "dir", cachefiles_daemon_dir },
{ "frun", cachefiles_daemon_frun },
{ "fcull", cachefiles_daemon_fcull },
{ "fstop", cachefiles_daemon_fstop },
{ "inuse", cachefiles_daemon_inuse },
{ "secctx", cachefiles_daemon_secctx },
{ "tag", cachefiles_daemon_tag },
#ifdef CONFIG_CACHEFILES_ONDEMAND
{ "copen", cachefiles_ondemand_copen },
{ "restore", cachefiles_ondemand_restore },
#endif
{ "", NULL }
};
/*
* Prepare a cache for caching.
*/
static int cachefiles_daemon_open(struct inode *inode, struct file *file)
{
struct cachefiles_cache *cache;
_enter("");
/* only the superuser may do this */
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
/* the cachefiles device may only be open once at a time */
if (xchg(&cachefiles_open, 1) == 1)
return -EBUSY;
/* allocate a cache record */
cache = kzalloc_obj(struct cachefiles_cache);
if (!cache) {
cachefiles_open = 0;
return -ENOMEM;
}
mutex_init(&cache->daemon_mutex);
init_waitqueue_head(&cache->daemon_pollwq);
INIT_LIST_HEAD(&cache->volumes);
INIT_LIST_HEAD(&cache->object_list);
spin_lock_init(&cache->object_list_lock);
refcount_set(&cache->unbind_pincount, 1);
xa_init_flags(&cache->reqs, XA_FLAGS_ALLOC);
xa_init_flags(&cache->ondemand_ids, XA_FLAGS_ALLOC1);
/* set default caching limits
* - limit at 1% free space and/or free files
* - cull below 5% free space and/or free files
* - cease culling above 7% free space and/or free files
*/
cache->frun_percent = 7;
cache->fcull_percent = 5;
cache->fstop_percent = 1;
cache->brun_percent = 7;
cache->bcull_percent = 5;
cache->bstop_percent = 1;
file->private_data = cache;
cache->cachefilesd = file;
return 0;
}
void cachefiles_flush_reqs(struct cachefiles_cache *cache)
{
struct xarray *xa = &cache->reqs;
Annotation
- Immediate include surface: `linux/module.h`, `linux/init.h`, `linux/sched.h`, `linux/completion.h`, `linux/slab.h`, `linux/fs.h`, `linux/file.h`, `linux/namei.h`.
- Detected declarations: `struct cachefiles_daemon_cmd`, `function cachefiles_daemon_open`, `function cachefiles_flush_reqs`, `function cachefiles_put_unbind_pincount`, `function cachefiles_get_unbind_pincount`, `function cachefiles_daemon_release`, `function cachefiles_do_daemon_read`, `function cachefiles_daemon_read`, `function cachefiles_daemon_write`, `function cachefiles_daemon_poll`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.