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>
14 extern struct super_block * sysfs_sb;
16 static struct address_space_operations sysfs_aops = {
17 .readpage = simple_readpage,
18 .prepare_write = simple_prepare_write,
19 .commit_write = simple_commit_write
22 static struct backing_dev_info sysfs_backing_dev_info = {
23 .ra_pages = 0, /* No readahead */
24 .memory_backed = 1, /* Does not contribute to dirty memory */
27 struct inode * sysfs_new_inode(mode_t mode)
29 struct inode * inode = new_inode(sysfs_sb);
32 inode->i_uid = current->fsuid;
33 inode->i_gid = current->fsgid;
34 inode->i_blksize = PAGE_CACHE_SIZE;
36 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
37 inode->i_mapping->a_ops = &sysfs_aops;
38 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
43 int sysfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *))
46 struct inode * inode = NULL;
48 if (!dentry->d_inode) {
49 if ((inode = sysfs_new_inode(mode)))
63 d_instantiate(dentry, inode);
64 dget(dentry); /* Extra count - pin the dentry in core */
71 int sysfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
73 return sysfs_create(dentry, mode, NULL);
76 struct dentry * sysfs_get_dentry(struct dentry * parent, const char * name)
81 qstr.len = strlen(name);
82 qstr.hash = full_name_hash(name,qstr.len);
83 return lookup_hash(&qstr,parent);
86 void sysfs_hash_and_remove(struct dentry * dir, const char * name)
88 struct dentry * victim;
90 down(&dir->d_inode->i_sem);
91 victim = sysfs_get_dentry(dir,name);
92 if (!IS_ERR(victim)) {
93 /* make sure dentry is really there */
94 if (victim->d_inode &&
95 (victim->d_parent->d_inode == dir->d_inode)) {
96 pr_debug("sysfs: Removing %s (%d)\n", victim->d_name.name,
97 atomic_read(&victim->d_count));
100 /* release the target kobject in case of
103 if (S_ISLNK(victim->d_inode->i_mode))
104 kobject_put(victim->d_fsdata);
105 simple_unlink(dir->d_inode,victim);
108 * Drop reference from sysfs_get_dentry() above.
112 up(&dir->d_inode->i_sem);