^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/proc/inode.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1991, 1992 Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/pid_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/sysctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static void proc_evict_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct proc_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct ctl_table_header *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct proc_inode *ei = PROC_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) truncate_inode_pages_final(&inode->i_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) clear_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Stop tracking associated processes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (ei->pid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) proc_pid_evict_inode(ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ei->pid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /* Let go of any associated proc directory entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) de = ei->pde;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (de) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) pde_put(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ei->pde = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) head = ei->sysctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) RCU_INIT_POINTER(ei->sysctl, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) proc_sys_evict_inode(inode, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static struct kmem_cache *proc_inode_cachep __ro_after_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static struct kmem_cache *pde_opener_cache __ro_after_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static struct inode *proc_alloc_inode(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct proc_inode *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) ei = kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (!ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ei->pid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) ei->fd = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) ei->op.proc_get_link = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ei->pde = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ei->sysctl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ei->sysctl_entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) INIT_HLIST_NODE(&ei->sibling_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ei->ns_ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return &ei->vfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static void proc_free_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) kmem_cache_free(proc_inode_cachep, PROC_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static void init_once(void *foo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct proc_inode *ei = (struct proc_inode *) foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) inode_init_once(&ei->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) void __init proc_init_kmemcache(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) proc_inode_cachep = kmem_cache_create("proc_inode_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) sizeof(struct proc_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) 0, (SLAB_RECLAIM_ACCOUNT|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) SLAB_MEM_SPREAD|SLAB_ACCOUNT|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) SLAB_PANIC),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) init_once);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) pde_opener_cache =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) kmem_cache_create("pde_opener", sizeof(struct pde_opener), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) SLAB_ACCOUNT|SLAB_PANIC, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) proc_dir_entry_cache = kmem_cache_create_usercopy(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) "proc_dir_entry", SIZEOF_PDE, 0, SLAB_PANIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) offsetof(struct proc_dir_entry, inline_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) SIZEOF_PDE_INLINE_NAME, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) BUILD_BUG_ON(sizeof(struct proc_dir_entry) >= SIZEOF_PDE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct proc_inode *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct hlist_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct super_block *old_sb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) node = hlist_first_rcu(inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ei = hlist_entry(node, struct proc_inode, sibling_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) spin_lock(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) hlist_del_init_rcu(&ei->sibling_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) spin_unlock(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) inode = &ei->vfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if ((sb != old_sb) && !atomic_inc_not_zero(&sb->s_active))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) inode = igrab(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (sb != old_sb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (old_sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) deactivate_super(old_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) old_sb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (unlikely(!inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct dentry *dir = d_find_any_alias(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) d_invalidate(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) while ((dentry = d_find_alias(inode))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) d_invalidate(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (old_sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) deactivate_super(old_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static inline const char *hidepid2str(enum proc_hidepid v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) switch (v) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) case HIDEPID_OFF: return "off";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) case HIDEPID_NO_ACCESS: return "noaccess";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) case HIDEPID_INVISIBLE: return "invisible";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) case HIDEPID_NOT_PTRACEABLE: return "ptraceable";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) WARN_ONCE(1, "bad hide_pid value: %d\n", v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static int proc_show_options(struct seq_file *seq, struct dentry *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct proc_fs_info *fs_info = proc_sb_info(root->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (!gid_eq(fs_info->pid_gid, GLOBAL_ROOT_GID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, fs_info->pid_gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (fs_info->hide_pid != HIDEPID_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) seq_printf(seq, ",hidepid=%s", hidepid2str(fs_info->hide_pid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (fs_info->pidonly != PROC_PIDONLY_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) seq_printf(seq, ",subset=pid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) const struct super_operations proc_sops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) .alloc_inode = proc_alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) .free_inode = proc_free_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) .drop_inode = generic_delete_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) .evict_inode = proc_evict_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) .statfs = simple_statfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) .show_options = proc_show_options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) enum {BIAS = -1U<<31};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static inline int use_pde(struct proc_dir_entry *pde)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return likely(atomic_inc_unless_negative(&pde->in_use));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static void unuse_pde(struct proc_dir_entry *pde)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (unlikely(atomic_dec_return(&pde->in_use) == BIAS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) complete(pde->pde_unload_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* pde is locked on entry, unlocked on exit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) __releases(&pde->pde_unload_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * close() (proc_reg_release()) can't delete an entry and proceed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * ->release hook needs to be available at the right moment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * rmmod (remove_proc_entry() et al) can't delete an entry and proceed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * "struct file" needs to be available at the right moment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * Therefore, first process to enter this function does ->release() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * signals its completion to the other process which does nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (pdeo->closing) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* somebody else is doing that, just wait */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) DECLARE_COMPLETION_ONSTACK(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) pdeo->c = &c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) spin_unlock(&pde->pde_unload_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) wait_for_completion(&c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) struct completion *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) pdeo->closing = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) spin_unlock(&pde->pde_unload_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) file = pdeo->file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) pde->proc_ops->proc_release(file_inode(file), file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) spin_lock(&pde->pde_unload_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /* After ->release. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) list_del(&pdeo->lh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) c = pdeo->c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) spin_unlock(&pde->pde_unload_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (unlikely(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) complete(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) kmem_cache_free(pde_opener_cache, pdeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) void proc_entry_rundown(struct proc_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) DECLARE_COMPLETION_ONSTACK(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /* Wait until all existing callers into module are done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) de->pde_unload_completion = &c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (atomic_add_return(BIAS, &de->in_use) != BIAS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) wait_for_completion(&c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /* ->pde_openers list can't grow from now on. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) spin_lock(&de->pde_unload_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) while (!list_empty(&de->pde_openers)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct pde_opener *pdeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) close_pdeo(de, pdeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) spin_lock(&de->pde_unload_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) spin_unlock(&de->pde_unload_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static loff_t pde_lseek(struct proc_dir_entry *pde, struct file *file, loff_t offset, int whence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) typeof_member(struct proc_ops, proc_lseek) lseek;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) lseek = pde->proc_ops->proc_lseek;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (!lseek)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) lseek = default_llseek;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return lseek(file, offset, whence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct proc_dir_entry *pde = PDE(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) loff_t rv = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (pde_is_permanent(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return pde_lseek(pde, file, offset, whence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) } else if (use_pde(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) rv = pde_lseek(pde, file, offset, whence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) unuse_pde(pde);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static ssize_t proc_reg_read_iter(struct kiocb *iocb, struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct proc_dir_entry *pde = PDE(file_inode(iocb->ki_filp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (pde_is_permanent(pde))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return pde->proc_ops->proc_read_iter(iocb, iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (!use_pde(pde))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) ret = pde->proc_ops->proc_read_iter(iocb, iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) unuse_pde(pde);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static ssize_t pde_read(struct proc_dir_entry *pde, struct file *file, char __user *buf, size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) typeof_member(struct proc_ops, proc_read) read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) read = pde->proc_ops->proc_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return read(file, buf, count, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) struct proc_dir_entry *pde = PDE(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) ssize_t rv = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (pde_is_permanent(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return pde_read(pde, file, buf, count, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) } else if (use_pde(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) rv = pde_read(pde, file, buf, count, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) unuse_pde(pde);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static ssize_t pde_write(struct proc_dir_entry *pde, struct file *file, const char __user *buf, size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) typeof_member(struct proc_ops, proc_write) write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) write = pde->proc_ops->proc_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return write(file, buf, count, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) struct proc_dir_entry *pde = PDE(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) ssize_t rv = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (pde_is_permanent(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return pde_write(pde, file, buf, count, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) } else if (use_pde(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) rv = pde_write(pde, file, buf, count, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) unuse_pde(pde);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static __poll_t pde_poll(struct proc_dir_entry *pde, struct file *file, struct poll_table_struct *pts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) typeof_member(struct proc_ops, proc_poll) poll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) poll = pde->proc_ops->proc_poll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (poll)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return poll(file, pts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return DEFAULT_POLLMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static __poll_t proc_reg_poll(struct file *file, struct poll_table_struct *pts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) struct proc_dir_entry *pde = PDE(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) __poll_t rv = DEFAULT_POLLMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (pde_is_permanent(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return pde_poll(pde, file, pts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) } else if (use_pde(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) rv = pde_poll(pde, file, pts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) unuse_pde(pde);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static long pde_ioctl(struct proc_dir_entry *pde, struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) typeof_member(struct proc_ops, proc_ioctl) ioctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) ioctl = pde->proc_ops->proc_ioctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return ioctl(file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct proc_dir_entry *pde = PDE(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) long rv = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (pde_is_permanent(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return pde_ioctl(pde, file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) } else if (use_pde(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) rv = pde_ioctl(pde, file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) unuse_pde(pde);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static long pde_compat_ioctl(struct proc_dir_entry *pde, struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) typeof_member(struct proc_ops, proc_compat_ioctl) compat_ioctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) compat_ioctl = pde->proc_ops->proc_compat_ioctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (compat_ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return compat_ioctl(file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) struct proc_dir_entry *pde = PDE(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) long rv = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (pde_is_permanent(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) return pde_compat_ioctl(pde, file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) } else if (use_pde(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) rv = pde_compat_ioctl(pde, file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) unuse_pde(pde);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) static int pde_mmap(struct proc_dir_entry *pde, struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) typeof_member(struct proc_ops, proc_mmap) mmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) mmap = pde->proc_ops->proc_mmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (mmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return mmap(file, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) struct proc_dir_entry *pde = PDE(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) int rv = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) if (pde_is_permanent(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) return pde_mmap(pde, file, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) } else if (use_pde(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) rv = pde_mmap(pde, file, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) unuse_pde(pde);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) static unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) pde_get_unmapped_area(struct proc_dir_entry *pde, struct file *file, unsigned long orig_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) unsigned long len, unsigned long pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) typeof_member(struct proc_ops, proc_get_unmapped_area) get_area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) get_area = pde->proc_ops->proc_get_unmapped_area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (!get_area)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) get_area = current->mm->get_unmapped_area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (get_area)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return get_area(file, orig_addr, len, pgoff, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) return orig_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) unsigned long len, unsigned long pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) struct proc_dir_entry *pde = PDE(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) unsigned long rv = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (pde_is_permanent(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) return pde_get_unmapped_area(pde, file, orig_addr, len, pgoff, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) } else if (use_pde(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) rv = pde_get_unmapped_area(pde, file, orig_addr, len, pgoff, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) unuse_pde(pde);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static int proc_reg_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct proc_dir_entry *pde = PDE(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) int rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) typeof_member(struct proc_ops, proc_open) open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) typeof_member(struct proc_ops, proc_release) release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) struct pde_opener *pdeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (pde_is_permanent(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) open = pde->proc_ops->proc_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) rv = open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) if (fs_info->pidonly == PROC_PIDONLY_ON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * Ensure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * 1) PDE's ->release hook will be called no matter what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) * either normally by close()/->release, or forcefully by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * rmmod/remove_proc_entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) * 2) rmmod isn't blocked by opening file in /proc and sitting on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) * the descriptor (including "rmmod foo </proc/foo" scenario).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) * Save every "struct file" with custom ->release hook.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (!use_pde(pde))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) release = pde->proc_ops->proc_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (release) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) pdeo = kmem_cache_alloc(pde_opener_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (!pdeo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) rv = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) goto out_unuse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) open = pde->proc_ops->proc_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) rv = open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (release) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (rv == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) /* To know what to release. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) pdeo->file = file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) pdeo->closing = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) pdeo->c = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) spin_lock(&pde->pde_unload_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) list_add(&pdeo->lh, &pde->pde_openers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) spin_unlock(&pde->pde_unload_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) kmem_cache_free(pde_opener_cache, pdeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) out_unuse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) unuse_pde(pde);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) static int proc_reg_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) struct proc_dir_entry *pde = PDE(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) struct pde_opener *pdeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (pde_is_permanent(pde)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) typeof_member(struct proc_ops, proc_release) release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) release = pde->proc_ops->proc_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (release) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) return release(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) spin_lock(&pde->pde_unload_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) list_for_each_entry(pdeo, &pde->pde_openers, lh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (pdeo->file == file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) close_pdeo(pde, pdeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) spin_unlock(&pde->pde_unload_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) static const struct file_operations proc_reg_file_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) .llseek = proc_reg_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) .read = proc_reg_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) .write = proc_reg_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) .poll = proc_reg_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) .unlocked_ioctl = proc_reg_unlocked_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) .mmap = proc_reg_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) .get_unmapped_area = proc_reg_get_unmapped_area,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) .open = proc_reg_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) .release = proc_reg_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) static const struct file_operations proc_iter_file_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) .llseek = proc_reg_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) .read_iter = proc_reg_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) .write = proc_reg_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) .splice_read = generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) .poll = proc_reg_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) .unlocked_ioctl = proc_reg_unlocked_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) .mmap = proc_reg_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) .get_unmapped_area = proc_reg_get_unmapped_area,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) .open = proc_reg_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) .release = proc_reg_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) static const struct file_operations proc_reg_file_ops_compat = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) .llseek = proc_reg_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) .read = proc_reg_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) .write = proc_reg_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) .poll = proc_reg_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) .unlocked_ioctl = proc_reg_unlocked_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) .compat_ioctl = proc_reg_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) .mmap = proc_reg_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) .get_unmapped_area = proc_reg_get_unmapped_area,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) .open = proc_reg_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) .release = proc_reg_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) static const struct file_operations proc_iter_file_ops_compat = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) .llseek = proc_reg_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) .read_iter = proc_reg_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) .splice_read = generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) .write = proc_reg_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) .poll = proc_reg_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) .unlocked_ioctl = proc_reg_unlocked_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) .compat_ioctl = proc_reg_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) .mmap = proc_reg_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) .get_unmapped_area = proc_reg_get_unmapped_area,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) .open = proc_reg_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) .release = proc_reg_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) static void proc_put_link(void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) unuse_pde(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) static const char *proc_get_link(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) struct delayed_call *done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) struct proc_dir_entry *pde = PDE(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) if (!use_pde(pde))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) set_delayed_call(done, proc_put_link, pde);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return pde->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) const struct inode_operations proc_link_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) .get_link = proc_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) struct inode *inode = new_inode(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) pde_put(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) inode->i_ino = de->low_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) PROC_I(inode)->pde = de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (is_empty_pde(de)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) make_empty_dir_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) if (de->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) inode->i_mode = de->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) inode->i_uid = de->uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) inode->i_gid = de->gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) if (de->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) inode->i_size = de->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (de->nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) set_nlink(inode, de->nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) inode->i_op = de->proc_iops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (de->proc_ops->proc_read_iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) inode->i_fop = &proc_iter_file_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) inode->i_fop = &proc_reg_file_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) if (de->proc_ops->proc_compat_ioctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) if (de->proc_ops->proc_read_iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) inode->i_fop = &proc_iter_file_ops_compat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) inode->i_fop = &proc_reg_file_ops_compat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) } else if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) inode->i_op = de->proc_iops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) inode->i_fop = de->proc_dir_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) } else if (S_ISLNK(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) inode->i_op = de->proc_iops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) inode->i_fop = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }