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