2 * fs/sysfs/inode.c - basic sysfs inode and dentry operations
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 * This file is released under the GPLv2.
10 * Please see Documentation/filesystems/sysfs.txt for more information.
15 #include <linux/pagemap.h>
16 #include <linux/namei.h>
17 #include <linux/backing-dev.h>
18 #include <linux/capability.h>
19 #include <linux/errno.h>
20 #include <linux/sched.h>
23 extern struct super_block * sysfs_sb;
25 static const struct address_space_operations sysfs_aops = {
26 .readpage = simple_readpage,
27 .write_begin = simple_write_begin,
28 .write_end = simple_write_end,
31 static struct backing_dev_info sysfs_backing_dev_info = {
32 .ra_pages = 0, /* No readahead */
33 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
36 static const struct inode_operations sysfs_inode_operations ={
37 .setattr = sysfs_setattr,
40 int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
42 struct inode * inode = dentry->d_inode;
43 struct sysfs_dirent * sd = dentry->d_fsdata;
44 struct iattr * sd_iattr;
45 unsigned int ia_valid = iattr->ia_valid;
51 sd_iattr = sd->s_iattr;
53 error = inode_change_ok(inode, iattr);
57 error = inode_setattr(inode, iattr);
62 /* setting attributes for the first time, allocate now */
63 sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL);
66 /* assign default attributes */
67 sd_iattr->ia_mode = sd->s_mode;
70 sd_iattr->ia_atime = sd_iattr->ia_mtime = sd_iattr->ia_ctime = CURRENT_TIME;
71 sd->s_iattr = sd_iattr;
74 /* attributes were changed atleast once in past */
76 if (ia_valid & ATTR_UID)
77 sd_iattr->ia_uid = iattr->ia_uid;
78 if (ia_valid & ATTR_GID)
79 sd_iattr->ia_gid = iattr->ia_gid;
80 if (ia_valid & ATTR_ATIME)
81 sd_iattr->ia_atime = timespec_trunc(iattr->ia_atime,
82 inode->i_sb->s_time_gran);
83 if (ia_valid & ATTR_MTIME)
84 sd_iattr->ia_mtime = timespec_trunc(iattr->ia_mtime,
85 inode->i_sb->s_time_gran);
86 if (ia_valid & ATTR_CTIME)
87 sd_iattr->ia_ctime = timespec_trunc(iattr->ia_ctime,
88 inode->i_sb->s_time_gran);
89 if (ia_valid & ATTR_MODE) {
90 umode_t mode = iattr->ia_mode;
92 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
94 sd_iattr->ia_mode = sd->s_mode = mode;
100 static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
102 inode->i_mode = mode;
105 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
108 static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
110 inode->i_mode = iattr->ia_mode;
111 inode->i_uid = iattr->ia_uid;
112 inode->i_gid = iattr->ia_gid;
113 inode->i_atime = iattr->ia_atime;
114 inode->i_mtime = iattr->ia_mtime;
115 inode->i_ctime = iattr->ia_ctime;
120 * sysfs has a different i_mutex lock order behavior for i_mutex than other
121 * filesystems; sysfs i_mutex is called in many places with subsystem locks
122 * held. At the same time, many of the VFS locking rules do not apply to
123 * sysfs at all (cross directory rename for example). To untangle this mess
124 * (which gives false positives in lockdep), we're giving sysfs inodes their
125 * own class for i_mutex.
127 static struct lock_class_key sysfs_inode_imutex_key;
129 static int sysfs_count_nlink(struct sysfs_dirent *sd)
131 struct sysfs_dirent *child;
134 for (child = sd->s_dir.children; child; child = child->s_sibling)
135 if (sysfs_type(child) == SYSFS_DIR)
141 static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
143 struct bin_attribute *bin_attr;
146 inode->i_mapping->a_ops = &sysfs_aops;
147 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
148 inode->i_op = &sysfs_inode_operations;
149 inode->i_ino = sd->s_ino;
150 lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key);
153 /* sysfs_dirent has non-default attributes
154 * get them for the new inode from persistent copy
157 set_inode_attr(inode, sd->s_iattr);
159 set_default_inode_attr(inode, sd->s_mode);
162 /* initialize inode according to type */
163 switch (sysfs_type(sd)) {
165 inode->i_op = &sysfs_dir_inode_operations;
166 inode->i_fop = &sysfs_dir_operations;
167 inode->i_nlink = sysfs_count_nlink(sd);
169 case SYSFS_KOBJ_ATTR:
170 inode->i_size = PAGE_SIZE;
171 inode->i_fop = &sysfs_file_operations;
173 case SYSFS_KOBJ_BIN_ATTR:
174 bin_attr = sd->s_bin_attr.bin_attr;
175 inode->i_size = bin_attr->size;
176 inode->i_fop = &bin_fops;
178 case SYSFS_KOBJ_LINK:
179 inode->i_op = &sysfs_symlink_inode_operations;
185 unlock_new_inode(inode);
189 * sysfs_get_inode - get inode for sysfs_dirent
190 * @sd: sysfs_dirent to allocate inode for
192 * Get inode for @sd. If such inode doesn't exist, a new inode
193 * is allocated and basics are initialized. New inode is
197 * Kernel thread context (may sleep).
200 * Pointer to allocated inode on success, NULL on failure.
202 struct inode * sysfs_get_inode(struct sysfs_dirent *sd)
206 inode = iget_locked(sysfs_sb, sd->s_ino);
207 if (inode && (inode->i_state & I_NEW))
208 sysfs_init_inode(sd, inode);
213 int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
215 struct sysfs_addrm_cxt acxt;
216 struct sysfs_dirent *sd;
221 sysfs_addrm_start(&acxt, dir_sd);
223 sd = sysfs_find_dirent(dir_sd, name);
225 sysfs_remove_one(&acxt, sd);
227 sysfs_addrm_finish(&acxt);