sysfs: make sysfs_dirent->s_element a union
[safe/jmp/linux-2.6] / fs / sysfs / sysfs.h
1 struct sysfs_elem_dir {
2         struct kobject          * kobj;
3 };
4
5 struct sysfs_elem_symlink {
6         struct kobject          * target_kobj;
7 };
8
9 struct sysfs_elem_attr {
10         struct attribute        * attr;
11 };
12
13 struct sysfs_elem_bin_attr {
14         struct bin_attribute    * bin_attr;
15 };
16
17 struct sysfs_dirent {
18         atomic_t                s_count;
19         struct sysfs_dirent     * s_parent;
20         struct list_head        s_sibling;
21         struct list_head        s_children;
22         const char              * s_name;
23
24         union {
25                 struct sysfs_elem_dir           dir;
26                 struct sysfs_elem_symlink       symlink;
27                 struct sysfs_elem_attr          attr;
28                 struct sysfs_elem_bin_attr      bin_attr;
29         }                       s_elem;
30
31         int                     s_type;
32         umode_t                 s_mode;
33         ino_t                   s_ino;
34         struct dentry           * s_dentry;
35         struct iattr            * s_iattr;
36         atomic_t                s_event;
37 };
38
39 extern struct vfsmount * sysfs_mount;
40 extern struct kmem_cache *sysfs_dir_cachep;
41
42 extern void sysfs_delete_inode(struct inode *inode);
43 extern struct inode * sysfs_new_inode(mode_t mode, struct sysfs_dirent *);
44 extern int sysfs_create(struct dentry *, int mode, int (*init)(struct inode *));
45
46 extern void release_sysfs_dirent(struct sysfs_dirent * sd);
47 extern int sysfs_dirent_exist(struct sysfs_dirent *, const unsigned char *);
48 extern struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode,
49                                              int type);
50 extern void sysfs_attach_dirent(struct sysfs_dirent *sd,
51                                 struct sysfs_dirent *parent_sd,
52                                 struct dentry *dentry);
53
54 extern int sysfs_add_file(struct dentry *, const struct attribute *, int);
55 extern int sysfs_hash_and_remove(struct dentry * dir, const char * name);
56 extern struct sysfs_dirent *sysfs_find(struct sysfs_dirent *dir, const char * name);
57
58 extern int sysfs_create_subdir(struct kobject *, const char *, struct dentry **);
59 extern void sysfs_remove_subdir(struct dentry *);
60
61 extern void sysfs_drop_dentry(struct sysfs_dirent *sd, struct dentry *parent);
62 extern int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
63
64 extern spinlock_t sysfs_lock;
65 extern struct rw_semaphore sysfs_rename_sem;
66 extern struct super_block * sysfs_sb;
67 extern const struct file_operations sysfs_dir_operations;
68 extern const struct file_operations sysfs_file_operations;
69 extern const struct file_operations bin_fops;
70 extern const struct inode_operations sysfs_dir_inode_operations;
71 extern const struct inode_operations sysfs_symlink_inode_operations;
72
73 struct sysfs_buffer {
74         struct list_head                associates;
75         size_t                          count;
76         loff_t                          pos;
77         char                            * page;
78         struct sysfs_ops                * ops;
79         struct semaphore                sem;
80         int                             orphaned;
81         int                             needs_read_fill;
82         int                             event;
83 };
84
85 struct sysfs_buffer_collection {
86         struct list_head        associates;
87 };
88
89 static inline struct kobject * to_kobj(struct dentry * dentry)
90 {
91         struct sysfs_dirent * sd = dentry->d_fsdata;
92         return sd->s_elem.dir.kobj;
93 }
94
95 static inline struct kobject *sysfs_get_kobject(struct dentry *dentry)
96 {
97         struct kobject * kobj = NULL;
98
99         spin_lock(&dcache_lock);
100         if (!d_unhashed(dentry)) {
101                 struct sysfs_dirent * sd = dentry->d_fsdata;
102                 if (sd->s_type & SYSFS_KOBJ_LINK)
103                         kobj = kobject_get(sd->s_elem.symlink.target_kobj);
104                 else
105                         kobj = kobject_get(sd->s_elem.dir.kobj);
106         }
107         spin_unlock(&dcache_lock);
108
109         return kobj;
110 }
111
112 static inline struct sysfs_dirent * sysfs_get(struct sysfs_dirent * sd)
113 {
114         if (sd) {
115                 WARN_ON(!atomic_read(&sd->s_count));
116                 atomic_inc(&sd->s_count);
117         }
118         return sd;
119 }
120
121 static inline void sysfs_put(struct sysfs_dirent * sd)
122 {
123         if (sd && atomic_dec_and_test(&sd->s_count))
124                 release_sysfs_dirent(sd);
125 }
126
127 static inline int sysfs_is_shadowed_inode(struct inode *inode)
128 {
129         return S_ISDIR(inode->i_mode) && inode->i_op->follow_link;
130 }