Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * dir.c - Operations for sysfs directories. |
| 3 | */ |
| 4 | |
| 5 | #undef DEBUG |
| 6 | |
| 7 | #include <linux/fs.h> |
| 8 | #include <linux/mount.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/kobject.h> |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 11 | #include <linux/namei.h> |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 12 | #include <linux/idr.h> |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 13 | #include <asm/semaphore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "sysfs.h" |
| 15 | |
| 16 | DECLARE_RWSEM(sysfs_rename_sem); |
Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 17 | spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 19 | static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED; |
| 20 | static DEFINE_IDA(sysfs_ino_ida); |
| 21 | |
| 22 | int sysfs_alloc_ino(ino_t *pino) |
| 23 | { |
| 24 | int ino, rc; |
| 25 | |
| 26 | retry: |
| 27 | spin_lock(&sysfs_ino_lock); |
| 28 | rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino); |
| 29 | spin_unlock(&sysfs_ino_lock); |
| 30 | |
| 31 | if (rc == -EAGAIN) { |
| 32 | if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL)) |
| 33 | goto retry; |
| 34 | rc = -ENOMEM; |
| 35 | } |
| 36 | |
| 37 | *pino = ino; |
| 38 | return rc; |
| 39 | } |
| 40 | |
| 41 | static void sysfs_free_ino(ino_t ino) |
| 42 | { |
| 43 | spin_lock(&sysfs_ino_lock); |
| 44 | ida_remove(&sysfs_ino_ida, ino); |
| 45 | spin_unlock(&sysfs_ino_lock); |
| 46 | } |
| 47 | |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 48 | void release_sysfs_dirent(struct sysfs_dirent * sd) |
| 49 | { |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 50 | struct sysfs_dirent *parent_sd; |
| 51 | |
| 52 | repeat: |
| 53 | parent_sd = sd->s_parent; |
| 54 | |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 55 | if (sd->s_type & SYSFS_KOBJ_LINK) |
| 56 | kobject_put(sd->s_elem.symlink.target_kobj); |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 57 | if (sd->s_type & SYSFS_COPY_NAME) |
| 58 | kfree(sd->s_name); |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 59 | kfree(sd->s_iattr); |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 60 | sysfs_free_ino(sd->s_ino); |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 61 | kmem_cache_free(sysfs_dir_cachep, sd); |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 62 | |
| 63 | sd = parent_sd; |
| 64 | if (sd && atomic_dec_and_test(&sd->s_count)) |
| 65 | goto repeat; |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 66 | } |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static void sysfs_d_iput(struct dentry * dentry, struct inode * inode) |
| 69 | { |
| 70 | struct sysfs_dirent * sd = dentry->d_fsdata; |
| 71 | |
| 72 | if (sd) { |
Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 73 | /* sd->s_dentry is protected with sysfs_lock. This |
| 74 | * allows sysfs_drop_dentry() to dereference it. |
| 75 | */ |
| 76 | spin_lock(&sysfs_lock); |
| 77 | |
| 78 | /* The dentry might have been deleted or another |
| 79 | * lookup could have happened updating sd->s_dentry to |
| 80 | * point the new dentry. Ignore if it isn't pointing |
| 81 | * to this dentry. |
| 82 | */ |
| 83 | if (sd->s_dentry == dentry) |
| 84 | sd->s_dentry = NULL; |
| 85 | spin_unlock(&sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | sysfs_put(sd); |
| 87 | } |
| 88 | iput(inode); |
| 89 | } |
| 90 | |
| 91 | static struct dentry_operations sysfs_dentry_ops = { |
| 92 | .d_iput = sysfs_d_iput, |
| 93 | }; |
| 94 | |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 95 | struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 97 | char *dup_name = NULL; |
| 98 | struct sysfs_dirent *sd = NULL; |
| 99 | |
| 100 | if (type & SYSFS_COPY_NAME) { |
| 101 | name = dup_name = kstrdup(name, GFP_KERNEL); |
| 102 | if (!name) |
| 103 | goto err_out; |
| 104 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 106 | sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | if (!sd) |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 108 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 110 | if (sysfs_alloc_ino(&sd->s_ino)) |
| 111 | goto err_out; |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | atomic_set(&sd->s_count, 1); |
Juha Yrjölä | eea3f89 | 2006-08-03 19:06:25 +0300 | [diff] [blame] | 114 | atomic_set(&sd->s_event, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | INIT_LIST_HEAD(&sd->s_children); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 116 | INIT_LIST_HEAD(&sd->s_sibling); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 117 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 118 | sd->s_name = name; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 119 | sd->s_mode = mode; |
| 120 | sd->s_type = type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | return sd; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 123 | |
| 124 | err_out: |
| 125 | kfree(dup_name); |
| 126 | kmem_cache_free(sysfs_dir_cachep, sd); |
| 127 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 130 | void sysfs_attach_dirent(struct sysfs_dirent *sd, |
| 131 | struct sysfs_dirent *parent_sd, struct dentry *dentry) |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 132 | { |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 133 | if (dentry) { |
| 134 | sd->s_dentry = dentry; |
| 135 | dentry->d_fsdata = sysfs_get(sd); |
| 136 | dentry->d_op = &sysfs_dentry_ops; |
| 137 | } |
| 138 | |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 139 | if (parent_sd) { |
| 140 | sd->s_parent = sysfs_get(parent_sd); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 141 | list_add(&sd->s_sibling, &parent_sd->s_children); |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 142 | } |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Martin Waitz | a580290 | 2006-04-02 13:59:55 +0200 | [diff] [blame] | 145 | /* |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 146 | * |
| 147 | * Return -EEXIST if there is already a sysfs element with the same name for |
| 148 | * the same parent. |
| 149 | * |
| 150 | * called with parent inode's i_mutex held |
| 151 | */ |
| 152 | int sysfs_dirent_exist(struct sysfs_dirent *parent_sd, |
| 153 | const unsigned char *new) |
| 154 | { |
| 155 | struct sysfs_dirent * sd; |
| 156 | |
| 157 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 158 | if (sd->s_type) { |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 159 | if (strcmp(sd->s_name, new)) |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 160 | continue; |
| 161 | else |
| 162 | return -EEXIST; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | static int init_dir(struct inode * inode) |
| 170 | { |
| 171 | inode->i_op = &sysfs_dir_inode_operations; |
| 172 | inode->i_fop = &sysfs_dir_operations; |
| 173 | |
| 174 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 175 | inc_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int init_file(struct inode * inode) |
| 180 | { |
| 181 | inode->i_size = PAGE_SIZE; |
| 182 | inode->i_fop = &sysfs_file_operations; |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int init_symlink(struct inode * inode) |
| 187 | { |
| 188 | inode->i_op = &sysfs_symlink_inode_operations; |
| 189 | return 0; |
| 190 | } |
| 191 | |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 192 | static int create_dir(struct kobject *kobj, struct dentry *parent, |
| 193 | const char *name, struct dentry **p_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
| 195 | int error; |
| 196 | umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 197 | struct dentry *dentry; |
| 198 | struct sysfs_dirent *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 200 | mutex_lock(&parent->d_inode->i_mutex); |
| 201 | |
| 202 | dentry = lookup_one_len(name, parent, strlen(name)); |
| 203 | if (IS_ERR(dentry)) { |
| 204 | error = PTR_ERR(dentry); |
| 205 | goto out_unlock; |
| 206 | } |
| 207 | |
| 208 | error = -EEXIST; |
| 209 | if (sysfs_dirent_exist(parent->d_fsdata, name)) |
| 210 | goto out_dput; |
| 211 | |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 212 | error = -ENOMEM; |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 213 | sd = sysfs_new_dirent(name, mode, SYSFS_DIR); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 214 | if (!sd) |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 215 | goto out_drop; |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 216 | sd->s_elem.dir.kobj = kobj; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 217 | sysfs_attach_dirent(sd, parent->d_fsdata, dentry); |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 218 | |
| 219 | error = sysfs_create(dentry, mode, init_dir); |
| 220 | if (error) |
| 221 | goto out_sput; |
| 222 | |
| 223 | inc_nlink(parent->d_inode); |
| 224 | dentry->d_op = &sysfs_dentry_ops; |
| 225 | d_rehash(dentry); |
| 226 | |
| 227 | *p_dentry = dentry; |
| 228 | error = 0; |
| 229 | goto out_dput; |
| 230 | |
| 231 | out_sput: |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 232 | list_del_init(&sd->s_sibling); |
| 233 | sysfs_put(sd); |
| 234 | out_drop: |
| 235 | d_drop(dentry); |
| 236 | out_dput: |
| 237 | dput(dentry); |
| 238 | out_unlock: |
| 239 | mutex_unlock(&parent->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return error; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d) |
| 245 | { |
| 246 | return create_dir(k,k->dentry,n,d); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * sysfs_create_dir - create a directory for an object. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | * @kobj: object we're creating directory for. |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 252 | * @shadow_parent: parent parent object. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | */ |
| 254 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 255 | int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
| 257 | struct dentry * dentry = NULL; |
| 258 | struct dentry * parent; |
| 259 | int error = 0; |
| 260 | |
| 261 | BUG_ON(!kobj); |
| 262 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 263 | if (shadow_parent) |
| 264 | parent = shadow_parent; |
| 265 | else if (kobj->parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | parent = kobj->parent->dentry; |
| 267 | else if (sysfs_mount && sysfs_mount->mnt_sb) |
| 268 | parent = sysfs_mount->mnt_sb->s_root; |
| 269 | else |
| 270 | return -EFAULT; |
| 271 | |
| 272 | error = create_dir(kobj,parent,kobject_name(kobj),&dentry); |
| 273 | if (!error) |
| 274 | kobj->dentry = dentry; |
| 275 | return error; |
| 276 | } |
| 277 | |
| 278 | /* attaches attribute's sysfs_dirent to the dentry corresponding to the |
| 279 | * attribute file |
| 280 | */ |
| 281 | static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry) |
| 282 | { |
| 283 | struct attribute * attr = NULL; |
| 284 | struct bin_attribute * bin_attr = NULL; |
| 285 | int (* init) (struct inode *) = NULL; |
| 286 | int error = 0; |
| 287 | |
| 288 | if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 289 | bin_attr = sd->s_elem.bin_attr.bin_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | attr = &bin_attr->attr; |
| 291 | } else { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 292 | attr = sd->s_elem.attr.attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | init = init_file; |
| 294 | } |
| 295 | |
Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 296 | dentry->d_fsdata = sysfs_get(sd); |
Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 297 | /* protect sd->s_dentry against sysfs_d_iput */ |
| 298 | spin_lock(&sysfs_lock); |
Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 299 | sd->s_dentry = dentry; |
Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 300 | spin_unlock(&sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init); |
Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 302 | if (error) { |
| 303 | sysfs_put(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | return error; |
Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
| 307 | if (bin_attr) { |
| 308 | dentry->d_inode->i_size = bin_attr->size; |
| 309 | dentry->d_inode->i_fop = &bin_fops; |
| 310 | } |
| 311 | dentry->d_op = &sysfs_dentry_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | d_rehash(dentry); |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry) |
| 318 | { |
| 319 | int err = 0; |
| 320 | |
Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 321 | dentry->d_fsdata = sysfs_get(sd); |
Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 322 | /* protect sd->s_dentry against sysfs_d_iput */ |
| 323 | spin_lock(&sysfs_lock); |
Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 324 | sd->s_dentry = dentry; |
Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 325 | spin_unlock(&sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink); |
| 327 | if (!err) { |
| 328 | dentry->d_op = &sysfs_dentry_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | d_rehash(dentry); |
Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 330 | } else |
| 331 | sysfs_put(sd); |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | return err; |
| 334 | } |
| 335 | |
| 336 | static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, |
| 337 | struct nameidata *nd) |
| 338 | { |
| 339 | struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata; |
| 340 | struct sysfs_dirent * sd; |
| 341 | int err = 0; |
| 342 | |
| 343 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { |
| 344 | if (sd->s_type & SYSFS_NOT_PINNED) { |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 345 | if (strcmp(sd->s_name, dentry->d_name.name)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | continue; |
| 347 | |
| 348 | if (sd->s_type & SYSFS_KOBJ_LINK) |
| 349 | err = sysfs_attach_link(sd, dentry); |
| 350 | else |
| 351 | err = sysfs_attach_attr(sd, dentry); |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return ERR_PTR(err); |
| 357 | } |
| 358 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 359 | const struct inode_operations sysfs_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | .lookup = sysfs_lookup, |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 361 | .setattr = sysfs_setattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | static void remove_dir(struct dentry * d) |
| 365 | { |
| 366 | struct dentry * parent = dget(d->d_parent); |
| 367 | struct sysfs_dirent * sd; |
| 368 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 369 | mutex_lock(&parent->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | d_delete(d); |
| 371 | sd = d->d_fsdata; |
| 372 | list_del_init(&sd->s_sibling); |
| 373 | sysfs_put(sd); |
| 374 | if (d->d_inode) |
| 375 | simple_rmdir(parent->d_inode,d); |
| 376 | |
| 377 | pr_debug(" o %s removing done (%d)\n",d->d_name.name, |
| 378 | atomic_read(&d->d_count)); |
| 379 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 380 | mutex_unlock(&parent->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | dput(parent); |
| 382 | } |
| 383 | |
| 384 | void sysfs_remove_subdir(struct dentry * d) |
| 385 | { |
| 386 | remove_dir(d); |
| 387 | } |
| 388 | |
| 389 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 390 | static void __sysfs_remove_dir(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | struct sysfs_dirent * parent_sd; |
| 393 | struct sysfs_dirent * sd, * tmp; |
| 394 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 395 | dget(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | if (!dentry) |
| 397 | return; |
| 398 | |
| 399 | pr_debug("sysfs %s: removing dir\n",dentry->d_name.name); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 400 | mutex_lock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | parent_sd = dentry->d_fsdata; |
| 402 | list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 403 | if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | continue; |
| 405 | list_del_init(&sd->s_sibling); |
| 406 | sysfs_drop_dentry(sd, dentry); |
| 407 | sysfs_put(sd); |
| 408 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 409 | mutex_unlock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | remove_dir(dentry); |
| 412 | /** |
| 413 | * Drop reference from dget() on entrance. |
| 414 | */ |
| 415 | dput(dentry); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /** |
| 419 | * sysfs_remove_dir - remove an object's directory. |
| 420 | * @kobj: object. |
| 421 | * |
| 422 | * The only thing special about this is that we remove any files in |
| 423 | * the directory before we remove the directory, and we've inlined |
| 424 | * what used to be sysfs_rmdir() below, instead of calling separately. |
| 425 | */ |
| 426 | |
| 427 | void sysfs_remove_dir(struct kobject * kobj) |
| 428 | { |
| 429 | __sysfs_remove_dir(kobj->dentry); |
Greg Kroah-Hartman | 641e6f3 | 2006-03-16 15:44:26 -0800 | [diff] [blame] | 430 | kobj->dentry = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 433 | int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent, |
| 434 | const char *new_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 436 | struct sysfs_dirent *sd = kobj->dentry->d_fsdata; |
| 437 | struct sysfs_dirent *parent_sd = new_parent->d_fsdata; |
| 438 | struct dentry *new_dentry; |
| 439 | char *dup_name; |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 440 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 442 | if (!new_parent) |
| 443 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
| 445 | down_write(&sysfs_rename_sem); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 446 | mutex_lock(&new_parent->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 448 | new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name)); |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 449 | if (IS_ERR(new_dentry)) { |
| 450 | error = PTR_ERR(new_dentry); |
| 451 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 453 | |
| 454 | /* By allowing two different directories with the same |
| 455 | * d_parent we allow this routine to move between different |
| 456 | * shadows of the same directory |
| 457 | */ |
| 458 | error = -EINVAL; |
| 459 | if (kobj->dentry->d_parent->d_inode != new_parent->d_inode || |
| 460 | new_dentry->d_parent->d_inode != new_parent->d_inode || |
| 461 | new_dentry == kobj->dentry) |
| 462 | goto out_dput; |
| 463 | |
| 464 | error = -EEXIST; |
| 465 | if (new_dentry->d_inode) |
| 466 | goto out_dput; |
| 467 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 468 | /* rename kobject and sysfs_dirent */ |
| 469 | error = -ENOMEM; |
| 470 | new_name = dup_name = kstrdup(new_name, GFP_KERNEL); |
| 471 | if (!new_name) |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 472 | goto out_drop; |
| 473 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 474 | error = kobject_set_name(kobj, "%s", new_name); |
| 475 | if (error) |
| 476 | goto out_free; |
| 477 | |
| 478 | kfree(sd->s_name); |
| 479 | sd->s_name = new_name; |
| 480 | |
| 481 | /* move under the new parent */ |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 482 | d_add(new_dentry, NULL); |
| 483 | d_move(kobj->dentry, new_dentry); |
| 484 | |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 485 | list_del_init(&sd->s_sibling); |
| 486 | list_add(&sd->s_sibling, &parent_sd->s_children); |
| 487 | |
| 488 | error = 0; |
| 489 | goto out_unlock; |
| 490 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 491 | out_free: |
| 492 | kfree(dup_name); |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 493 | out_drop: |
| 494 | d_drop(new_dentry); |
| 495 | out_dput: |
| 496 | dput(new_dentry); |
| 497 | out_unlock: |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 498 | mutex_unlock(&new_parent->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | up_write(&sysfs_rename_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | return error; |
| 501 | } |
| 502 | |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 503 | int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent) |
| 504 | { |
| 505 | struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry; |
| 506 | struct sysfs_dirent *new_parent_sd, *sd; |
| 507 | int error; |
| 508 | |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 509 | old_parent_dentry = kobj->parent ? |
| 510 | kobj->parent->dentry : sysfs_mount->mnt_sb->s_root; |
Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 511 | new_parent_dentry = new_parent ? |
| 512 | new_parent->dentry : sysfs_mount->mnt_sb->s_root; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 513 | |
Mark Lord | 0de1517 | 2007-03-06 01:42:03 -0800 | [diff] [blame] | 514 | if (old_parent_dentry->d_inode == new_parent_dentry->d_inode) |
| 515 | return 0; /* nothing to move */ |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 516 | again: |
| 517 | mutex_lock(&old_parent_dentry->d_inode->i_mutex); |
| 518 | if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) { |
| 519 | mutex_unlock(&old_parent_dentry->d_inode->i_mutex); |
| 520 | goto again; |
| 521 | } |
| 522 | |
| 523 | new_parent_sd = new_parent_dentry->d_fsdata; |
| 524 | sd = kobj->dentry->d_fsdata; |
| 525 | |
| 526 | new_dentry = lookup_one_len(kobj->name, new_parent_dentry, |
| 527 | strlen(kobj->name)); |
| 528 | if (IS_ERR(new_dentry)) { |
| 529 | error = PTR_ERR(new_dentry); |
| 530 | goto out; |
| 531 | } else |
| 532 | error = 0; |
| 533 | d_add(new_dentry, NULL); |
| 534 | d_move(kobj->dentry, new_dentry); |
| 535 | dput(new_dentry); |
| 536 | |
| 537 | /* Remove from old parent's list and insert into new parent's list. */ |
| 538 | list_del_init(&sd->s_sibling); |
| 539 | list_add(&sd->s_sibling, &new_parent_sd->s_children); |
| 540 | |
| 541 | out: |
| 542 | mutex_unlock(&new_parent_dentry->d_inode->i_mutex); |
| 543 | mutex_unlock(&old_parent_dentry->d_inode->i_mutex); |
| 544 | |
| 545 | return error; |
| 546 | } |
| 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | static int sysfs_dir_open(struct inode *inode, struct file *file) |
| 549 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 550 | struct dentry * dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 552 | struct sysfs_dirent * sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 554 | mutex_lock(&dentry->d_inode->i_mutex); |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 555 | sd = sysfs_new_dirent("_DIR_", 0, 0); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 556 | if (sd) |
| 557 | sysfs_attach_dirent(sd, parent_sd, NULL); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 558 | mutex_unlock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 560 | file->private_data = sd; |
| 561 | return sd ? 0 : -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static int sysfs_dir_close(struct inode *inode, struct file *file) |
| 565 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 566 | struct dentry * dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | struct sysfs_dirent * cursor = file->private_data; |
| 568 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 569 | mutex_lock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | list_del_init(&cursor->s_sibling); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 571 | mutex_unlock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
| 573 | release_sysfs_dirent(cursor); |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | /* Relationship between s_mode and the DT_xxx types */ |
| 579 | static inline unsigned char dt_type(struct sysfs_dirent *sd) |
| 580 | { |
| 581 | return (sd->s_mode >> 12) & 15; |
| 582 | } |
| 583 | |
| 584 | static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) |
| 585 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 586 | struct dentry *dentry = filp->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; |
| 588 | struct sysfs_dirent *cursor = filp->private_data; |
| 589 | struct list_head *p, *q = &cursor->s_sibling; |
| 590 | ino_t ino; |
| 591 | int i = filp->f_pos; |
| 592 | |
| 593 | switch (i) { |
| 594 | case 0: |
Eric Sandeen | dc35125 | 2007-06-11 14:02:45 +0900 | [diff] [blame] | 595 | ino = parent_sd->s_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) |
| 597 | break; |
| 598 | filp->f_pos++; |
| 599 | i++; |
| 600 | /* fallthrough */ |
| 601 | case 1: |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 602 | if (parent_sd->s_parent) |
| 603 | ino = parent_sd->s_parent->s_ino; |
| 604 | else |
| 605 | ino = parent_sd->s_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) |
| 607 | break; |
| 608 | filp->f_pos++; |
| 609 | i++; |
| 610 | /* fallthrough */ |
| 611 | default: |
Akinobu Mita | 1bfba4e | 2006-06-26 00:24:40 -0700 | [diff] [blame] | 612 | if (filp->f_pos == 2) |
| 613 | list_move(q, &parent_sd->s_children); |
| 614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | for (p=q->next; p!= &parent_sd->s_children; p=p->next) { |
| 616 | struct sysfs_dirent *next; |
| 617 | const char * name; |
| 618 | int len; |
| 619 | |
| 620 | next = list_entry(p, struct sysfs_dirent, |
| 621 | s_sibling); |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 622 | if (!next->s_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | continue; |
| 624 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 625 | name = next->s_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | len = strlen(name); |
Eric Sandeen | dc35125 | 2007-06-11 14:02:45 +0900 | [diff] [blame] | 627 | ino = next->s_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
| 629 | if (filldir(dirent, name, len, filp->f_pos, ino, |
| 630 | dt_type(next)) < 0) |
| 631 | return 0; |
| 632 | |
Akinobu Mita | 1bfba4e | 2006-06-26 00:24:40 -0700 | [diff] [blame] | 633 | list_move(q, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | p = q; |
| 635 | filp->f_pos++; |
| 636 | } |
| 637 | } |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin) |
| 642 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 643 | struct dentry * dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 645 | mutex_lock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | switch (origin) { |
| 647 | case 1: |
| 648 | offset += file->f_pos; |
| 649 | case 0: |
| 650 | if (offset >= 0) |
| 651 | break; |
| 652 | default: |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 653 | mutex_unlock(&file->f_path.dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | return -EINVAL; |
| 655 | } |
| 656 | if (offset != file->f_pos) { |
| 657 | file->f_pos = offset; |
| 658 | if (file->f_pos >= 2) { |
| 659 | struct sysfs_dirent *sd = dentry->d_fsdata; |
| 660 | struct sysfs_dirent *cursor = file->private_data; |
| 661 | struct list_head *p; |
| 662 | loff_t n = file->f_pos - 2; |
| 663 | |
| 664 | list_del(&cursor->s_sibling); |
| 665 | p = sd->s_children.next; |
| 666 | while (n && p != &sd->s_children) { |
| 667 | struct sysfs_dirent *next; |
| 668 | next = list_entry(p, struct sysfs_dirent, |
| 669 | s_sibling); |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 670 | if (next->s_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | n--; |
| 672 | p = p->next; |
| 673 | } |
| 674 | list_add_tail(&cursor->s_sibling, p); |
| 675 | } |
| 676 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 677 | mutex_unlock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | return offset; |
| 679 | } |
| 680 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 681 | |
| 682 | /** |
| 683 | * sysfs_make_shadowed_dir - Setup so a directory can be shadowed |
| 684 | * @kobj: object we're creating shadow of. |
| 685 | */ |
| 686 | |
| 687 | int sysfs_make_shadowed_dir(struct kobject *kobj, |
| 688 | void * (*follow_link)(struct dentry *, struct nameidata *)) |
| 689 | { |
| 690 | struct inode *inode; |
| 691 | struct inode_operations *i_op; |
| 692 | |
| 693 | inode = kobj->dentry->d_inode; |
| 694 | if (inode->i_op != &sysfs_dir_inode_operations) |
| 695 | return -EINVAL; |
| 696 | |
| 697 | i_op = kmalloc(sizeof(*i_op), GFP_KERNEL); |
| 698 | if (!i_op) |
| 699 | return -ENOMEM; |
| 700 | |
| 701 | memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op)); |
| 702 | i_op->follow_link = follow_link; |
| 703 | |
| 704 | /* Locking of inode->i_op? |
| 705 | * Since setting i_op is a single word write and they |
| 706 | * are atomic we should be ok here. |
| 707 | */ |
| 708 | inode->i_op = i_op; |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * sysfs_create_shadow_dir - create a shadow directory for an object. |
| 714 | * @kobj: object we're creating directory for. |
| 715 | * |
| 716 | * sysfs_make_shadowed_dir must already have been called on this |
| 717 | * directory. |
| 718 | */ |
| 719 | |
| 720 | struct dentry *sysfs_create_shadow_dir(struct kobject *kobj) |
| 721 | { |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 722 | struct dentry *dir = kobj->dentry; |
| 723 | struct inode *inode = dir->d_inode; |
| 724 | struct dentry *parent = dir->d_parent; |
| 725 | struct sysfs_dirent *parent_sd = parent->d_fsdata; |
| 726 | struct dentry *shadow; |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 727 | struct sysfs_dirent *sd; |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 728 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 729 | shadow = ERR_PTR(-EINVAL); |
| 730 | if (!sysfs_is_shadowed_inode(inode)) |
| 731 | goto out; |
| 732 | |
| 733 | shadow = d_alloc(parent, &dir->d_name); |
| 734 | if (!shadow) |
| 735 | goto nomem; |
| 736 | |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 737 | sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 738 | if (!sd) |
| 739 | goto nomem; |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 740 | sd->s_elem.dir.kobj = kobj; |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 741 | /* point to parent_sd but don't attach to it */ |
| 742 | sd->s_parent = sysfs_get(parent_sd); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 743 | sysfs_attach_dirent(sd, NULL, shadow); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 744 | |
| 745 | d_instantiate(shadow, igrab(inode)); |
| 746 | inc_nlink(inode); |
| 747 | inc_nlink(parent->d_inode); |
| 748 | shadow->d_op = &sysfs_dentry_ops; |
| 749 | |
| 750 | dget(shadow); /* Extra count - pin the dentry in core */ |
| 751 | |
| 752 | out: |
| 753 | return shadow; |
| 754 | nomem: |
| 755 | dput(shadow); |
| 756 | shadow = ERR_PTR(-ENOMEM); |
| 757 | goto out; |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * sysfs_remove_shadow_dir - remove an object's directory. |
| 762 | * @shadow: dentry of shadow directory |
| 763 | * |
| 764 | * The only thing special about this is that we remove any files in |
| 765 | * the directory before we remove the directory, and we've inlined |
| 766 | * what used to be sysfs_rmdir() below, instead of calling separately. |
| 767 | */ |
| 768 | |
| 769 | void sysfs_remove_shadow_dir(struct dentry *shadow) |
| 770 | { |
| 771 | __sysfs_remove_dir(shadow); |
| 772 | } |
| 773 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 774 | const struct file_operations sysfs_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | .open = sysfs_dir_open, |
| 776 | .release = sysfs_dir_close, |
| 777 | .llseek = sysfs_dir_lseek, |
| 778 | .read = generic_read_dir, |
| 779 | .readdir = sysfs_readdir, |
| 780 | }; |