blob: 466943d576f16631a9bda2ec7fb391fca5552553 [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/*
137 * inode.c
138 */
139struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd);
140void sysfs_evict_inode(struct inode *inode);
141int sysfs_permission(struct inode *inode, int mask);
142int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
143int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
144 struct kstat *stat);
145int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
146 size_t size, int flags);
147int sysfs_inode_init(void);
148
Tejun Heofd7b9f72013-11-28 14:54:33 -0500149/*
150 * dir.c
151 */
152extern struct mutex sysfs_mutex;
153extern const struct dentry_operations sysfs_dentry_ops;
154extern const struct file_operations sysfs_dir_operations;
155extern const struct inode_operations sysfs_dir_inode_operations;
156
157struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd);
158void sysfs_put_active(struct sysfs_dirent *sd);
159void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt);
160int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
161 struct sysfs_dirent *parent_sd);
162void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt);
Tejun Heobc755552013-11-28 14:54:41 -0500163struct sysfs_dirent *sysfs_new_dirent(struct kernfs_root *root,
164 const char *name, umode_t mode, int type);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500165
Tejun Heo414985a2013-11-28 14:54:34 -0500166/*
167 * file.c
168 */
169extern const struct file_operations kernfs_file_operations;
170
171void sysfs_unmap_bin_file(struct sysfs_dirent *sd);
172
Tejun Heo2072f1a2013-11-28 14:54:35 -0500173/*
174 * symlink.c
175 */
176extern const struct inode_operations sysfs_symlink_inode_operations;
177
Tejun Heoae6621b2013-11-28 14:54:31 -0500178#endif /* __KERNFS_INTERNAL_H */