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 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
19 extern struct super_block * sysfs_sb;
21 static const struct address_space_operations sysfs_aops = {
22 .readpage = simple_readpage,
23 .prepare_write = simple_prepare_write,
24 .commit_write = simple_commit_write
27 static struct backing_dev_info sysfs_backing_dev_info = {
28 .ra_pages = 0, /* No readahead */
29 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
32 static const struct inode_operations sysfs_inode_operations ={
33 .setattr = sysfs_setattr,
36 int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
38 struct inode * inode = dentry->d_inode;
39 struct sysfs_dirent * sd = dentry->d_fsdata;
40 struct iattr * sd_iattr;
41 unsigned int ia_valid = iattr->ia_valid;
47 sd_iattr = sd->s_iattr;
49 error = inode_change_ok(inode, iattr);
53 error = inode_setattr(inode, iattr);
58 /* setting attributes for the first time, allocate now */
59 sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL);
62 /* assign default attributes */
63 sd_iattr->ia_mode = sd->s_mode;
66 sd_iattr->ia_atime = sd_iattr->ia_mtime = sd_iattr->ia_ctime = CURRENT_TIME;
67 sd->s_iattr = sd_iattr;
70 /* attributes were changed atleast once in past */
72 if (ia_valid & ATTR_UID)
73 sd_iattr->ia_uid = iattr->ia_uid;
74 if (ia_valid & ATTR_GID)
75 sd_iattr->ia_gid = iattr->ia_gid;
76 if (ia_valid & ATTR_ATIME)
77 sd_iattr->ia_atime = timespec_trunc(iattr->ia_atime,
78 inode->i_sb->s_time_gran);
79 if (ia_valid & ATTR_MTIME)
80 sd_iattr->ia_mtime = timespec_trunc(iattr->ia_mtime,
81 inode->i_sb->s_time_gran);
82 if (ia_valid & ATTR_CTIME)
83 sd_iattr->ia_ctime = timespec_trunc(iattr->ia_ctime,
84 inode->i_sb->s_time_gran);
85 if (ia_valid & ATTR_MODE) {
86 umode_t mode = iattr->ia_mode;
88 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
90 sd_iattr->ia_mode = sd->s_mode = mode;
96 static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
101 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
104 static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
106 inode->i_mode = iattr->ia_mode;
107 inode->i_uid = iattr->ia_uid;
108 inode->i_gid = iattr->ia_gid;
109 inode->i_atime = iattr->ia_atime;
110 inode->i_mtime = iattr->ia_mtime;
111 inode->i_ctime = iattr->ia_ctime;
116 * sysfs has a different i_mutex lock order behavior for i_mutex than other
117 * filesystems; sysfs i_mutex is called in many places with subsystem locks
118 * held. At the same time, many of the VFS locking rules do not apply to
119 * sysfs at all (cross directory rename for example). To untangle this mess
120 * (which gives false positives in lockdep), we're giving sysfs inodes their
121 * own class for i_mutex.
123 static struct lock_class_key sysfs_inode_imutex_key;
125 static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
128 inode->i_mapping->a_ops = &sysfs_aops;
129 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
130 inode->i_op = &sysfs_inode_operations;
131 inode->i_ino = sd->s_ino;
132 lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key);
135 /* sysfs_dirent has non-default attributes
136 * get them for the new inode from persistent copy
139 set_inode_attr(inode, sd->s_iattr);
141 set_default_inode_attr(inode, sd->s_mode);
145 * sysfs_get_inode - get inode for sysfs_dirent
146 * @sd: sysfs_dirent to allocate inode for
148 * Get inode for @sd. If such inode doesn't exist, a new inode
149 * is allocated and basics are initialized. New inode is
153 * Kernel thread context (may sleep).
156 * Pointer to allocated inode on success, NULL on failure.
158 struct inode * sysfs_get_inode(struct sysfs_dirent *sd)
162 inode = iget_locked(sysfs_sb, sd->s_ino);
163 if (inode && (inode->i_state & I_NEW))
164 sysfs_init_inode(sd, inode);
170 * sysfs_instantiate - instantiate dentry
171 * @dentry: dentry to be instantiated
172 * @inode: inode associated with @sd
174 * Unlock @inode if locked and instantiate @dentry with @inode.
179 void sysfs_instantiate(struct dentry *dentry, struct inode *inode)
181 BUG_ON(!dentry || dentry->d_inode);
183 if (inode->i_state & I_NEW)
184 unlock_new_inode(inode);
186 d_instantiate(dentry, inode);
189 int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
191 struct sysfs_addrm_cxt acxt;
192 struct sysfs_dirent *sd;
197 sysfs_addrm_start(&acxt, dir_sd);
199 sd = sysfs_find_dirent(dir_sd, name);
201 sysfs_remove_one(&acxt, sd);
203 sysfs_addrm_finish(&acxt);