xref: /linux-6.15/include/linux/proc_fs.h (revision 2d6ffcca)
1 #ifndef _LINUX_PROC_FS_H
2 #define _LINUX_PROC_FS_H
3 
4 #include <linux/slab.h>
5 #include <linux/fs.h>
6 #include <linux/spinlock.h>
7 #include <linux/magic.h>
8 #include <asm/atomic.h>
9 
10 struct net;
11 struct completion;
12 struct mm_struct;
13 
14 /*
15  * The proc filesystem constants/structures
16  */
17 
18 /*
19  * Offset of the first process in the /proc root directory..
20  */
21 #define FIRST_PROCESS_ENTRY 256
22 
23 /* Worst case buffer size needed for holding an integer. */
24 #define PROC_NUMBUF 13
25 
26 /*
27  * We always define these enumerators
28  */
29 
30 enum {
31 	PROC_ROOT_INO = 1,
32 };
33 
34 /*
35  * This is not completely implemented yet. The idea is to
36  * create an in-memory tree (like the actual /proc filesystem
37  * tree) of these proc_dir_entries, so that we can dynamically
38  * add new files to /proc.
39  *
40  * The "next" pointer creates a linked list of one /proc directory,
41  * while parent/subdir create the directory structure (every
42  * /proc file has a parent, but "subdir" is NULL for all
43  * non-directory entries).
44  *
45  * "owner" is used to protect module
46  * from unloading while proc_dir_entry is in use
47  */
48 
49 typedef	int (read_proc_t)(char *page, char **start, off_t off,
50 			  int count, int *eof, void *data);
51 typedef	int (write_proc_t)(struct file *file, const char __user *buffer,
52 			   unsigned long count, void *data);
53 
54 struct proc_dir_entry {
55 	unsigned int low_ino;
56 	unsigned short namelen;
57 	const char *name;
58 	mode_t mode;
59 	nlink_t nlink;
60 	uid_t uid;
61 	gid_t gid;
62 	loff_t size;
63 	const struct inode_operations *proc_iops;
64 	/*
65 	 * NULL ->proc_fops means "PDE is going away RSN" or
66 	 * "PDE is just created". In either case, e.g. ->read_proc won't be
67 	 * called because it's too late or too early, respectively.
68 	 *
69 	 * If you're allocating ->proc_fops dynamically, save a pointer
70 	 * somewhere.
71 	 */
72 	const struct file_operations *proc_fops;
73 	struct module *owner;
74 	struct proc_dir_entry *next, *parent, *subdir;
75 	void *data;
76 	read_proc_t *read_proc;
77 	write_proc_t *write_proc;
78 	atomic_t count;		/* use count */
79 	int pde_users;	/* number of callers into module in progress */
80 	spinlock_t pde_unload_lock; /* proc_fops checks and pde_users bumps */
81 	struct completion *pde_unload_completion;
82 };
83 
84 struct kcore_list {
85 	struct kcore_list *next;
86 	unsigned long addr;
87 	size_t size;
88 };
89 
90 struct vmcore {
91 	struct list_head list;
92 	unsigned long long paddr;
93 	unsigned long long size;
94 	loff_t offset;
95 };
96 
97 #ifdef CONFIG_PROC_FS
98 
99 extern struct proc_dir_entry *proc_root_kcore;
100 
101 extern spinlock_t proc_subdir_lock;
102 
103 extern void proc_root_init(void);
104 extern void proc_misc_init(void);
105 
106 void proc_flush_task(struct task_struct *task);
107 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *);
108 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir);
109 unsigned long task_vsize(struct mm_struct *);
110 int task_statm(struct mm_struct *, int *, int *, int *, int *);
111 void task_mem(struct seq_file *, struct mm_struct *);
112 void clear_refs_smap(struct mm_struct *mm);
113 
114 struct proc_dir_entry *de_get(struct proc_dir_entry *de);
115 void de_put(struct proc_dir_entry *de);
116 
117 extern struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
118 						struct proc_dir_entry *parent);
119 struct proc_dir_entry *proc_create_data(const char *name, mode_t mode,
120 				struct proc_dir_entry *parent,
121 				const struct file_operations *proc_fops,
122 				void *data);
123 extern void remove_proc_entry(const char *name, struct proc_dir_entry *parent);
124 
125 extern struct vfsmount *proc_mnt;
126 struct pid_namespace;
127 extern int proc_fill_super(struct super_block *);
128 extern struct inode *proc_get_inode(struct super_block *, unsigned int, struct proc_dir_entry *);
129 
130 /*
131  * These are generic /proc routines that use the internal
132  * "struct proc_dir_entry" tree to traverse the filesystem.
133  *
134  * The /proc root directory has extended versions to take care
135  * of the /proc/<pid> subdirectories.
136  */
137 extern int proc_readdir(struct file *, void *, filldir_t);
138 extern struct dentry *proc_lookup(struct inode *, struct dentry *, struct nameidata *);
139 
140 extern const struct file_operations proc_kcore_operations;
141 extern const struct file_operations proc_kmsg_operations;
142 extern const struct file_operations ppc_htab_operations;
143 
144 extern int pid_ns_prepare_proc(struct pid_namespace *ns);
145 extern void pid_ns_release_proc(struct pid_namespace *ns);
146 
147 /*
148  * proc_tty.c
149  */
150 struct tty_driver;
151 extern void proc_tty_init(void);
152 extern void proc_tty_register_driver(struct tty_driver *driver);
153 extern void proc_tty_unregister_driver(struct tty_driver *driver);
154 
155 /*
156  * proc_devtree.c
157  */
158 #ifdef CONFIG_PROC_DEVICETREE
159 struct device_node;
160 struct property;
161 extern void proc_device_tree_init(void);
162 extern void proc_device_tree_add_node(struct device_node *, struct proc_dir_entry *);
163 extern void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop);
164 extern void proc_device_tree_remove_prop(struct proc_dir_entry *pde,
165 					 struct property *prop);
166 extern void proc_device_tree_update_prop(struct proc_dir_entry *pde,
167 					 struct property *newprop,
168 					 struct property *oldprop);
169 #endif /* CONFIG_PROC_DEVICETREE */
170 
171 extern struct proc_dir_entry *proc_symlink(const char *,
172 		struct proc_dir_entry *, const char *);
173 extern struct proc_dir_entry *proc_mkdir(const char *,struct proc_dir_entry *);
174 extern struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
175 			struct proc_dir_entry *parent);
176 
177 static inline struct proc_dir_entry *proc_create(const char *name, mode_t mode,
178 	struct proc_dir_entry *parent, const struct file_operations *proc_fops)
179 {
180 	return proc_create_data(name, mode, parent, proc_fops, NULL);
181 }
182 
183 static inline struct proc_dir_entry *create_proc_read_entry(const char *name,
184 	mode_t mode, struct proc_dir_entry *base,
185 	read_proc_t *read_proc, void * data)
186 {
187 	struct proc_dir_entry *res=create_proc_entry(name,mode,base);
188 	if (res) {
189 		res->read_proc=read_proc;
190 		res->data=data;
191 	}
192 	return res;
193 }
194 
195 extern struct proc_dir_entry *proc_net_fops_create(struct net *net,
196 	const char *name, mode_t mode, const struct file_operations *fops);
197 extern void proc_net_remove(struct net *net, const char *name);
198 extern struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name,
199 	struct proc_dir_entry *parent);
200 
201 /* While the {get|set|dup}_mm_exe_file functions are for mm_structs, they are
202  * only needed to implement /proc/<pid>|self/exe so we define them here. */
203 extern void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file);
204 extern struct file *get_mm_exe_file(struct mm_struct *mm);
205 extern void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm);
206 
207 #else
208 
209 #define proc_net_fops_create(net, name, mode, fops)  ({ (void)(mode), NULL; })
210 static inline void proc_net_remove(struct net *net, const char *name) {}
211 
212 static inline void proc_flush_task(struct task_struct *task)
213 {
214 }
215 
216 static inline struct proc_dir_entry *create_proc_entry(const char *name,
217 	mode_t mode, struct proc_dir_entry *parent) { return NULL; }
218 static inline struct proc_dir_entry *proc_create(const char *name,
219 	mode_t mode, struct proc_dir_entry *parent,
220 	const struct file_operations *proc_fops)
221 {
222 	return NULL;
223 }
224 static inline struct proc_dir_entry *proc_create_data(const char *name,
225 	mode_t mode, struct proc_dir_entry *parent,
226 	const struct file_operations *proc_fops, void *data)
227 {
228 	return NULL;
229 }
230 #define remove_proc_entry(name, parent) do {} while (0)
231 
232 static inline struct proc_dir_entry *proc_symlink(const char *name,
233 		struct proc_dir_entry *parent,const char *dest) {return NULL;}
234 static inline struct proc_dir_entry *proc_mkdir(const char *name,
235 	struct proc_dir_entry *parent) {return NULL;}
236 
237 static inline struct proc_dir_entry *create_proc_read_entry(const char *name,
238 	mode_t mode, struct proc_dir_entry *base,
239 	read_proc_t *read_proc, void * data) { return NULL; }
240 
241 struct tty_driver;
242 static inline void proc_tty_register_driver(struct tty_driver *driver) {};
243 static inline void proc_tty_unregister_driver(struct tty_driver *driver) {};
244 
245 static inline int pid_ns_prepare_proc(struct pid_namespace *ns)
246 {
247 	return 0;
248 }
249 
250 static inline void pid_ns_release_proc(struct pid_namespace *ns)
251 {
252 }
253 
254 static inline void set_mm_exe_file(struct mm_struct *mm,
255 				   struct file *new_exe_file)
256 {}
257 
258 static inline struct file *get_mm_exe_file(struct mm_struct *mm)
259 {
260 	return NULL;
261 }
262 
263 static inline void dup_mm_exe_file(struct mm_struct *oldmm,
264 	       			   struct mm_struct *newmm)
265 {}
266 
267 #endif /* CONFIG_PROC_FS */
268 
269 #if !defined(CONFIG_PROC_KCORE)
270 static inline void kclist_add(struct kcore_list *new, void *addr, size_t size)
271 {
272 }
273 #else
274 extern void kclist_add(struct kcore_list *, void *, size_t);
275 #endif
276 
277 union proc_op {
278 	int (*proc_get_link)(struct inode *, struct path *);
279 	int (*proc_read)(struct task_struct *task, char *page);
280 	int (*proc_show)(struct seq_file *m,
281 		struct pid_namespace *ns, struct pid *pid,
282 		struct task_struct *task);
283 };
284 
285 struct proc_inode {
286 	struct pid *pid;
287 	int fd;
288 	union proc_op op;
289 	struct proc_dir_entry *pde;
290 	struct inode vfs_inode;
291 };
292 
293 static inline struct proc_inode *PROC_I(const struct inode *inode)
294 {
295 	return container_of(inode, struct proc_inode, vfs_inode);
296 }
297 
298 static inline struct proc_dir_entry *PDE(const struct inode *inode)
299 {
300 	return PROC_I(inode)->pde;
301 }
302 
303 static inline struct net *PDE_NET(struct proc_dir_entry *pde)
304 {
305 	return pde->parent->data;
306 }
307 
308 struct proc_maps_private {
309 	struct pid *pid;
310 	struct task_struct *task;
311 #ifdef CONFIG_MMU
312 	struct vm_area_struct *tail_vma;
313 #endif
314 };
315 
316 #endif /* _LINUX_PROC_FS_H */
317