blob: ced0d6dadc7dec972562b5d286dfde5ee388813f [file] [log] [blame]
Tejun Heoae6621b2013-11-28 14:54:31 -05001/*
2 * fs/kernfs/kernfs-internal.h - kernfs internal header file
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 */
10
11#ifndef __KERNFS_INTERNAL_H
12#define __KERNFS_INTERNAL_H
13
14#include <linux/lockdep.h>
15#include <linux/fs.h>
16#include <linux/rbtree.h>
Tejun Heofd7b9f72013-11-28 14:54:33 -050017#include <linux/mutex.h>
Tejun Heoae6621b2013-11-28 14:54:31 -050018
19#include <linux/kernfs.h>
20
21struct sysfs_open_dirent;
22
23/* type-specific structures for sysfs_dirent->s_* union members */
24struct sysfs_elem_dir {
25 unsigned long subdirs;
26 /* children rbtree starts here and goes through sd->s_rb */
27 struct rb_root children;
Tejun Heoba7443b2013-11-28 14:54:40 -050028
29 /*
30 * The kernfs hierarchy this directory belongs to. This fits
31 * better directly in sysfs_dirent but is here to save space.
32 */
33 struct kernfs_root *root;
Tejun Heoae6621b2013-11-28 14:54:31 -050034};
35
36struct sysfs_elem_symlink {
37 struct sysfs_dirent *target_sd;
38};
39
40struct sysfs_elem_attr {
41 const struct kernfs_ops *ops;
42 struct sysfs_open_dirent *open;
43 loff_t size;
44};
45
46struct sysfs_inode_attrs {
47 struct iattr ia_iattr;
48 void *ia_secdata;
49 u32 ia_secdata_len;
50};
51
52/*
53 * sysfs_dirent - the building block of sysfs hierarchy. Each and
54 * every sysfs node is represented by single sysfs_dirent.
55 *
56 * As long as s_count reference is held, the sysfs_dirent itself is
57 * accessible. Dereferencing s_elem or any other outer entity
58 * requires s_active reference.
59 */
60struct sysfs_dirent {
61 atomic_t s_count;
62 atomic_t s_active;
63#ifdef CONFIG_DEBUG_LOCK_ALLOC
64 struct lockdep_map dep_map;
65#endif
66 struct sysfs_dirent *s_parent;
67 const char *s_name;
68
69 struct rb_node s_rb;
70
71 union {
72 struct completion *completion;
73 struct sysfs_dirent *removed_list;
74 } u;
75
76 const void *s_ns; /* namespace tag */
77 unsigned int s_hash; /* ns + name hash */
78 union {
79 struct sysfs_elem_dir s_dir;
80 struct sysfs_elem_symlink s_symlink;
81 struct sysfs_elem_attr s_attr;
82 };
83
84 void *priv;
85
86 unsigned short s_flags;
87 umode_t s_mode;
88 unsigned int s_ino;
89 struct sysfs_inode_attrs *s_iattr;
90};
91
92#define SD_DEACTIVATED_BIAS INT_MIN
93
94#define SYSFS_TYPE_MASK 0x000f
95#define SYSFS_DIR 0x0001
96#define SYSFS_KOBJ_ATTR 0x0002
97#define SYSFS_KOBJ_LINK 0x0004
98#define SYSFS_COPY_NAME (SYSFS_DIR | SYSFS_KOBJ_LINK)
99#define SYSFS_ACTIVE_REF SYSFS_KOBJ_ATTR
100
101#define SYSFS_FLAG_MASK ~SYSFS_TYPE_MASK
102#define SYSFS_FLAG_REMOVED 0x0010
103#define SYSFS_FLAG_NS 0x0020
104#define SYSFS_FLAG_HAS_SEQ_SHOW 0x0040
105#define SYSFS_FLAG_HAS_MMAP 0x0080
106#define SYSFS_FLAG_LOCKDEP 0x0100
107
108static inline unsigned int sysfs_type(struct sysfs_dirent *sd)
109{
110 return sd->s_flags & SYSFS_TYPE_MASK;
111}
112
Tejun Heoba7443b2013-11-28 14:54:40 -0500113/**
114 * kernfs_root - find out the kernfs_root a sysfs_dirent belongs to
115 * @sd: sysfs_dirent of interest
116 *
117 * Return the kernfs_root @sd belongs to.
118 */
119static inline struct kernfs_root *kernfs_root(struct sysfs_dirent *sd)
120{
121 /* if parent exists, it's always a dir; otherwise, @sd is a dir */
122 if (sd->s_parent)
123 sd = sd->s_parent;
124 return sd->s_dir.root;
125}
126
Tejun Heoae6621b2013-11-28 14:54:31 -0500127/*
128 * Context structure to be used while adding/removing nodes.
129 */
130struct sysfs_addrm_cxt {
131 struct sysfs_dirent *removed;
132};
133
134#include "../sysfs/sysfs.h"
135
Tejun Heoffed24e2013-11-28 14:54:32 -0500136/*
Tejun Heofa736a92013-11-28 14:54:44 -0500137 * mount.c
138 */
139struct sysfs_super_info {
140 /*
141 * The root associated with this super_block. Each super_block is
142 * identified by the root and ns it's associated with.
143 */
144 struct kernfs_root *root;
145
146 /*
147 * Each sb is associated with one namespace tag, currently the network
148 * namespace of the task which mounted this sysfs instance. If multiple
149 * tags become necessary, make the following an array and compare
150 * sysfs_dirent tag against every entry.
151 */
152 const void *ns;
153};
154#define sysfs_info(SB) ((struct sysfs_super_info *)(SB->s_fs_info))
155
156extern struct kmem_cache *sysfs_dir_cachep;
157
158/*
Tejun Heoffed24e2013-11-28 14:54:32 -0500159 * inode.c
160 */
161struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd);
162void sysfs_evict_inode(struct inode *inode);
163int sysfs_permission(struct inode *inode, int mask);
164int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
165int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
166 struct kstat *stat);
167int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
168 size_t size, int flags);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500169void sysfs_inode_init(void);
Tejun Heoffed24e2013-11-28 14:54:32 -0500170
Tejun Heofd7b9f72013-11-28 14:54:33 -0500171/*
172 * dir.c
173 */
174extern struct mutex sysfs_mutex;
175extern const struct dentry_operations sysfs_dentry_ops;
176extern const struct file_operations sysfs_dir_operations;
177extern const struct inode_operations sysfs_dir_inode_operations;
178
179struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd);
180void sysfs_put_active(struct sysfs_dirent *sd);
181void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt);
182int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
183 struct sysfs_dirent *parent_sd);
184void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt);
Tejun Heobc755552013-11-28 14:54:41 -0500185struct sysfs_dirent *sysfs_new_dirent(struct kernfs_root *root,
186 const char *name, umode_t mode, int type);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500187
Tejun Heo414985a2013-11-28 14:54:34 -0500188/*
189 * file.c
190 */
191extern const struct file_operations kernfs_file_operations;
192
193void sysfs_unmap_bin_file(struct sysfs_dirent *sd);
194
Tejun Heo2072f1a2013-11-28 14:54:35 -0500195/*
196 * symlink.c
197 */
198extern const struct inode_operations sysfs_symlink_inode_operations;
199
Tejun Heoae6621b2013-11-28 14:54:31 -0500200#endif /* __KERNFS_INTERNAL_H */