2 * inode.c - basic inode and dentry operations.
4 * sysfs is Copyright (c) 2001-3 Patrick Mochel
6 * Please see Documentation/filesystems/sysfs.txt for more information.
11 #include <linux/pagemap.h>
12 #include <linux/namei.h>
13 #include <linux/backing-dev.h>
16 extern struct super_block
* sysfs_sb
;
18 static struct address_space_operations sysfs_aops
= {
19 .readpage
= simple_readpage
,
20 .prepare_write
= simple_prepare_write
,
21 .commit_write
= simple_commit_write
24 static struct backing_dev_info sysfs_backing_dev_info
= {
25 .ra_pages
= 0, /* No readahead */
26 .capabilities
= BDI_CAP_NO_ACCT_DIRTY
| BDI_CAP_NO_WRITEBACK
,
29 struct inode
* sysfs_new_inode(mode_t mode
)
31 struct inode
* inode
= new_inode(sysfs_sb
);
36 inode
->i_blksize
= PAGE_CACHE_SIZE
;
38 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
39 inode
->i_mapping
->a_ops
= &sysfs_aops
;
40 inode
->i_mapping
->backing_dev_info
= &sysfs_backing_dev_info
;
45 int sysfs_create(struct dentry
* dentry
, int mode
, int (*init
)(struct inode
*))
48 struct inode
* inode
= NULL
;
50 if (!dentry
->d_inode
) {
51 if ((inode
= sysfs_new_inode(mode
))) {
52 if (dentry
->d_parent
&& dentry
->d_parent
->d_inode
) {
53 struct inode
*p_inode
= dentry
->d_parent
->d_inode
;
54 p_inode
->i_mtime
= p_inode
->i_ctime
= CURRENT_TIME
;
70 d_instantiate(dentry
, inode
);
72 dget(dentry
); /* pin only directory dentry in core */
79 struct dentry
* sysfs_get_dentry(struct dentry
* parent
, const char * name
)
84 qstr
.len
= strlen(name
);
85 qstr
.hash
= full_name_hash(name
,qstr
.len
);
86 return lookup_hash(&qstr
,parent
);
90 * Get the name for corresponding element represented by the given sysfs_dirent
92 const unsigned char * sysfs_get_name(struct sysfs_dirent
*sd
)
94 struct attribute
* attr
;
95 struct bin_attribute
* bin_attr
;
96 struct sysfs_symlink
* sl
;
98 if (!sd
|| !sd
->s_element
)
101 switch (sd
->s_type
) {
103 /* Always have a dentry so use that */
104 return sd
->s_dentry
->d_name
.name
;
106 case SYSFS_KOBJ_ATTR
:
107 attr
= sd
->s_element
;
110 case SYSFS_KOBJ_BIN_ATTR
:
111 bin_attr
= sd
->s_element
;
112 return bin_attr
->attr
.name
;
114 case SYSFS_KOBJ_LINK
:
116 return sl
->link_name
;
123 * Unhashes the dentry corresponding to given sysfs_dirent
124 * Called with parent inode's i_sem held.
126 void sysfs_drop_dentry(struct sysfs_dirent
* sd
, struct dentry
* parent
)
128 struct dentry
* dentry
= sd
->s_dentry
;
131 spin_lock(&dcache_lock
);
132 spin_lock(&dentry
->d_lock
);
133 if (!(d_unhashed(dentry
) && dentry
->d_inode
)) {
136 spin_unlock(&dentry
->d_lock
);
137 spin_unlock(&dcache_lock
);
138 simple_unlink(parent
->d_inode
, dentry
);
140 spin_unlock(&dentry
->d_lock
);
141 spin_unlock(&dcache_lock
);
146 void sysfs_hash_and_remove(struct dentry
* dir
, const char * name
)
148 struct sysfs_dirent
* sd
;
149 struct sysfs_dirent
* parent_sd
= dir
->d_fsdata
;
151 down(&dir
->d_inode
->i_sem
);
152 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
155 if (!strcmp(sysfs_get_name(sd
), name
)) {
156 list_del_init(&sd
->s_sibling
);
157 sysfs_drop_dentry(sd
, dir
);
162 up(&dir
->d_inode
->i_sem
);