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