fs/notify/fanotify/fanotify.c
Source file repositories/reference/linux-study-clean/fs/notify/fanotify/fanotify.c
File Facts
- System
- Linux kernel
- Corpus path
fs/notify/fanotify/fanotify.c- Extension
.c- Size
- 32547 bytes
- Lines
- 1122
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: implementation source
- Status
- source 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.
- 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/fanotify.hlinux/fsnotify_backend.hlinux/init.hlinux/jiffies.hlinux/kernel.hlinux/mount.hlinux/sched.hlinux/sched/user.hlinux/sched/signal.hlinux/types.hlinux/wait.hlinux/audit.hlinux/sched/mm.hlinux/statfs.hlinux/stringhash.hlinux/pidfs.hfanotify.h
Detected Declarations
function fanotify_path_equalfunction fanotify_hash_pathfunction fanotify_hash_fsidfunction fanotify_fh_equalfunction fanotify_hash_fhfunction fanotify_fid_event_equalfunction fanotify_info_equalfunction fanotify_name_event_equalfunction fanotify_error_event_equalfunction fanotify_should_mergefunction fanotify_mergefunction hlist_for_each_entryfunction eventfunction fanotify_group_event_maskfunction fsnotify_foreach_iter_mark_typefunction eventsfunction fanotify_encode_fh_lenfunction fanotify_encode_fhfunction fanotify_report_child_fidfunction fanotify_get_fsidfunction fsnotify_foreach_iter_mark_typefunction fanotify_insert_eventfunction fanotify_handle_eventfunction fanotify_free_group_privfunction fanotify_free_path_eventfunction fanotify_free_perm_eventfunction fanotify_free_fid_eventfunction fanotify_free_name_eventfunction fanotify_free_error_eventfunction fanotify_free_mnt_eventfunction fanotify_free_eventfunction fanotify_freeing_markfunction fanotify_free_mark
Annotated Snippet
if (fanotify_should_merge(old, new)) {
old->mask |= new->mask;
if (fanotify_is_error_event(old->mask))
FANOTIFY_EE(old)->err_count++;
return 1;
}
}
return 0;
}
/*
* Wait for response to permission event. The function also takes care of
* freeing the permission event (or offloads that in case the wait is canceled
* by a signal). The function returns 0 in case access got allowed by userspace,
* -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case
* the wait got interrupted by a signal.
*/
static int fanotify_get_response(struct fsnotify_group *group,
struct fanotify_perm_event *event,
struct fsnotify_iter_info *iter_info)
{
int ret, errno;
pr_debug("%s: group=%p event=%p\n", __func__, group, event);
ret = wait_event_state(group->fanotify_data.access_waitq,
event->state == FAN_EVENT_ANSWERED,
(TASK_KILLABLE|TASK_FREEZABLE));
/* Signal pending? */
if (ret < 0) {
spin_lock(&group->notification_lock);
/* Event reported to userspace and no answer yet? */
if (event->state == FAN_EVENT_REPORTED) {
/* Event will get freed once userspace answers to it */
event->state = FAN_EVENT_CANCELED;
spin_unlock(&group->notification_lock);
return ret;
}
/* Event not yet reported? Just remove it. */
if (event->state == FAN_EVENT_INIT) {
fsnotify_remove_queued_event(group, &event->fae.fse);
/* Permission events are not supposed to be hashed */
WARN_ON_ONCE(!hlist_unhashed(&event->fae.merge_list));
}
/*
* Event may be also answered in case signal delivery raced
* with wakeup. In that case we have nothing to do besides
* freeing the event and reporting error.
*/
spin_unlock(&group->notification_lock);
goto out;
}
/* userspace responded, convert to something usable */
switch (event->response & FANOTIFY_RESPONSE_ACCESS) {
case FAN_ALLOW:
ret = 0;
break;
case FAN_DENY:
/* Check custom errno from pre-content events */
errno = fanotify_get_response_errno(event->response);
if (errno) {
ret = -errno;
break;
}
fallthrough;
default:
ret = -EPERM;
}
/* Check if the response should be audited */
if (event->response & FAN_AUDIT) {
u32 response = event->response &
(FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS);
audit_fanotify(response & ~FAN_AUDIT, &event->audit_rule);
}
pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
group, event, ret);
out:
fsnotify_destroy_event(group, &event->fae.fse);
return ret;
}
/*
Annotation
- Immediate include surface: `linux/fanotify.h`, `linux/fsnotify_backend.h`, `linux/init.h`, `linux/jiffies.h`, `linux/kernel.h`, `linux/mount.h`, `linux/sched.h`, `linux/sched/user.h`.
- Detected declarations: `function fanotify_path_equal`, `function fanotify_hash_path`, `function fanotify_hash_fsid`, `function fanotify_fh_equal`, `function fanotify_hash_fh`, `function fanotify_fid_event_equal`, `function fanotify_info_equal`, `function fanotify_name_event_equal`, `function fanotify_error_event_equal`, `function fanotify_should_merge`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: source 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.