2 * dir.c - Operations for sysfs directories.
8 #include <linux/mount.h>
9 #include <linux/module.h>
10 #include <linux/kobject.h>
11 #include <linux/namei.h>
14 DECLARE_RWSEM(sysfs_rename_sem
);
16 static void sysfs_d_iput(struct dentry
* dentry
, struct inode
* inode
)
18 struct sysfs_dirent
* sd
= dentry
->d_fsdata
;
21 BUG_ON(sd
->s_dentry
!= dentry
);
28 static struct dentry_operations sysfs_dentry_ops
= {
29 .d_iput
= sysfs_d_iput
,
33 * Allocates a new sysfs_dirent and links it to the parent sysfs_dirent
35 static struct sysfs_dirent
* sysfs_new_dirent(struct sysfs_dirent
* parent_sd
,
38 struct sysfs_dirent
* sd
;
40 sd
= kmem_cache_alloc(sysfs_dir_cachep
, GFP_KERNEL
);
44 memset(sd
, 0, sizeof(*sd
));
45 atomic_set(&sd
->s_count
, 1);
46 atomic_set(&sd
->s_event
, 0);
47 INIT_LIST_HEAD(&sd
->s_children
);
48 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
49 sd
->s_element
= element
;
56 * Return -EEXIST if there is already a sysfs element with the same name for
59 * called with parent inode's i_mutex held
61 int sysfs_dirent_exist(struct sysfs_dirent
*parent_sd
,
62 const unsigned char *new)
64 struct sysfs_dirent
* sd
;
66 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
68 const unsigned char *existing
= sysfs_get_name(sd
);
69 if (strcmp(existing
, new))
80 int sysfs_make_dirent(struct sysfs_dirent
* parent_sd
, struct dentry
* dentry
,
81 void * element
, umode_t mode
, int type
)
83 struct sysfs_dirent
* sd
;
85 sd
= sysfs_new_dirent(parent_sd
, element
);
91 sd
->s_dentry
= dentry
;
93 dentry
->d_fsdata
= sysfs_get(sd
);
94 dentry
->d_op
= &sysfs_dentry_ops
;
100 static int init_dir(struct inode
* inode
)
102 inode
->i_op
= &sysfs_dir_inode_operations
;
103 inode
->i_fop
= &sysfs_dir_operations
;
105 /* directory inodes start off with i_nlink == 2 (for "." entry) */
110 static int init_file(struct inode
* inode
)
112 inode
->i_size
= PAGE_SIZE
;
113 inode
->i_fop
= &sysfs_file_operations
;
117 static int init_symlink(struct inode
* inode
)
119 inode
->i_op
= &sysfs_symlink_inode_operations
;
123 static int create_dir(struct kobject
* k
, struct dentry
* p
,
124 const char * n
, struct dentry
** d
)
127 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
129 mutex_lock(&p
->d_inode
->i_mutex
);
130 *d
= lookup_one_len(n
, p
, strlen(n
));
132 if (sysfs_dirent_exist(p
->d_fsdata
, n
))
135 error
= sysfs_make_dirent(p
->d_fsdata
, *d
, k
, mode
,
138 error
= sysfs_create(*d
, mode
, init_dir
);
140 p
->d_inode
->i_nlink
++;
141 (*d
)->d_op
= &sysfs_dentry_ops
;
145 if (error
&& (error
!= -EEXIST
)) {
146 struct sysfs_dirent
*sd
= (*d
)->d_fsdata
;
148 list_del_init(&sd
->s_sibling
);
156 mutex_unlock(&p
->d_inode
->i_mutex
);
161 int sysfs_create_subdir(struct kobject
* k
, const char * n
, struct dentry
** d
)
163 return create_dir(k
,k
->dentry
,n
,d
);
167 * sysfs_create_dir - create a directory for an object.
168 * @parent: parent parent object.
169 * @kobj: object we're creating directory for.
172 int sysfs_create_dir(struct kobject
* kobj
)
174 struct dentry
* dentry
= NULL
;
175 struct dentry
* parent
;
181 parent
= kobj
->parent
->dentry
;
182 else if (sysfs_mount
&& sysfs_mount
->mnt_sb
)
183 parent
= sysfs_mount
->mnt_sb
->s_root
;
187 error
= create_dir(kobj
,parent
,kobject_name(kobj
),&dentry
);
189 kobj
->dentry
= dentry
;
193 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
196 static int sysfs_attach_attr(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
198 struct attribute
* attr
= NULL
;
199 struct bin_attribute
* bin_attr
= NULL
;
200 int (* init
) (struct inode
*) = NULL
;
203 if (sd
->s_type
& SYSFS_KOBJ_BIN_ATTR
) {
204 bin_attr
= sd
->s_element
;
205 attr
= &bin_attr
->attr
;
207 attr
= sd
->s_element
;
211 dentry
->d_fsdata
= sysfs_get(sd
);
212 sd
->s_dentry
= dentry
;
213 error
= sysfs_create(dentry
, (attr
->mode
& S_IALLUGO
) | S_IFREG
, init
);
220 dentry
->d_inode
->i_size
= bin_attr
->size
;
221 dentry
->d_inode
->i_fop
= &bin_fops
;
223 dentry
->d_op
= &sysfs_dentry_ops
;
229 static int sysfs_attach_link(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
233 dentry
->d_fsdata
= sysfs_get(sd
);
234 sd
->s_dentry
= dentry
;
235 err
= sysfs_create(dentry
, S_IFLNK
|S_IRWXUGO
, init_symlink
);
237 dentry
->d_op
= &sysfs_dentry_ops
;
245 static struct dentry
* sysfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
246 struct nameidata
*nd
)
248 struct sysfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
249 struct sysfs_dirent
* sd
;
252 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
253 if (sd
->s_type
& SYSFS_NOT_PINNED
) {
254 const unsigned char * name
= sysfs_get_name(sd
);
256 if (strcmp(name
, dentry
->d_name
.name
))
259 if (sd
->s_type
& SYSFS_KOBJ_LINK
)
260 err
= sysfs_attach_link(sd
, dentry
);
262 err
= sysfs_attach_attr(sd
, dentry
);
270 struct inode_operations sysfs_dir_inode_operations
= {
271 .lookup
= sysfs_lookup
,
272 .setattr
= sysfs_setattr
,
275 static void remove_dir(struct dentry
* d
)
277 struct dentry
* parent
= dget(d
->d_parent
);
278 struct sysfs_dirent
* sd
;
280 mutex_lock(&parent
->d_inode
->i_mutex
);
283 list_del_init(&sd
->s_sibling
);
286 simple_rmdir(parent
->d_inode
,d
);
288 pr_debug(" o %s removing done (%d)\n",d
->d_name
.name
,
289 atomic_read(&d
->d_count
));
291 mutex_unlock(&parent
->d_inode
->i_mutex
);
295 void sysfs_remove_subdir(struct dentry
* d
)
302 * sysfs_remove_dir - remove an object's directory.
305 * The only thing special about this is that we remove any files in
306 * the directory before we remove the directory, and we've inlined
307 * what used to be sysfs_rmdir() below, instead of calling separately.
310 void sysfs_remove_dir(struct kobject
* kobj
)
312 struct dentry
* dentry
= dget(kobj
->dentry
);
313 struct sysfs_dirent
* parent_sd
;
314 struct sysfs_dirent
* sd
, * tmp
;
319 pr_debug("sysfs %s: removing dir\n",dentry
->d_name
.name
);
320 mutex_lock(&dentry
->d_inode
->i_mutex
);
321 parent_sd
= dentry
->d_fsdata
;
322 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
323 if (!sd
->s_element
|| !(sd
->s_type
& SYSFS_NOT_PINNED
))
325 list_del_init(&sd
->s_sibling
);
326 sysfs_drop_dentry(sd
, dentry
);
329 mutex_unlock(&dentry
->d_inode
->i_mutex
);
333 * Drop reference from dget() on entrance.
339 int sysfs_rename_dir(struct kobject
* kobj
, const char *new_name
)
342 struct dentry
* new_dentry
, * parent
;
344 if (!strcmp(kobject_name(kobj
), new_name
))
350 down_write(&sysfs_rename_sem
);
351 parent
= kobj
->parent
->dentry
;
353 mutex_lock(&parent
->d_inode
->i_mutex
);
355 new_dentry
= lookup_one_len(new_name
, parent
, strlen(new_name
));
356 if (!IS_ERR(new_dentry
)) {
357 if (!new_dentry
->d_inode
) {
358 error
= kobject_set_name(kobj
, "%s", new_name
);
360 d_add(new_dentry
, NULL
);
361 d_move(kobj
->dentry
, new_dentry
);
369 mutex_unlock(&parent
->d_inode
->i_mutex
);
370 up_write(&sysfs_rename_sem
);
375 static int sysfs_dir_open(struct inode
*inode
, struct file
*file
)
377 struct dentry
* dentry
= file
->f_dentry
;
378 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
380 mutex_lock(&dentry
->d_inode
->i_mutex
);
381 file
->private_data
= sysfs_new_dirent(parent_sd
, NULL
);
382 mutex_unlock(&dentry
->d_inode
->i_mutex
);
384 return file
->private_data
? 0 : -ENOMEM
;
388 static int sysfs_dir_close(struct inode
*inode
, struct file
*file
)
390 struct dentry
* dentry
= file
->f_dentry
;
391 struct sysfs_dirent
* cursor
= file
->private_data
;
393 mutex_lock(&dentry
->d_inode
->i_mutex
);
394 list_del_init(&cursor
->s_sibling
);
395 mutex_unlock(&dentry
->d_inode
->i_mutex
);
397 release_sysfs_dirent(cursor
);
402 /* Relationship between s_mode and the DT_xxx types */
403 static inline unsigned char dt_type(struct sysfs_dirent
*sd
)
405 return (sd
->s_mode
>> 12) & 15;
408 static int sysfs_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
410 struct dentry
*dentry
= filp
->f_dentry
;
411 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
412 struct sysfs_dirent
*cursor
= filp
->private_data
;
413 struct list_head
*p
, *q
= &cursor
->s_sibling
;
419 ino
= dentry
->d_inode
->i_ino
;
420 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
426 ino
= parent_ino(dentry
);
427 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
433 if (filp
->f_pos
== 2) {
435 list_add(q
, &parent_sd
->s_children
);
437 for (p
=q
->next
; p
!= &parent_sd
->s_children
; p
=p
->next
) {
438 struct sysfs_dirent
*next
;
442 next
= list_entry(p
, struct sysfs_dirent
,
444 if (!next
->s_element
)
447 name
= sysfs_get_name(next
);
450 ino
= next
->s_dentry
->d_inode
->i_ino
;
452 ino
= iunique(sysfs_sb
, 2);
454 if (filldir(dirent
, name
, len
, filp
->f_pos
, ino
,
467 static loff_t
sysfs_dir_lseek(struct file
* file
, loff_t offset
, int origin
)
469 struct dentry
* dentry
= file
->f_dentry
;
471 mutex_lock(&dentry
->d_inode
->i_mutex
);
474 offset
+= file
->f_pos
;
479 mutex_unlock(&file
->f_dentry
->d_inode
->i_mutex
);
482 if (offset
!= file
->f_pos
) {
483 file
->f_pos
= offset
;
484 if (file
->f_pos
>= 2) {
485 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
486 struct sysfs_dirent
*cursor
= file
->private_data
;
488 loff_t n
= file
->f_pos
- 2;
490 list_del(&cursor
->s_sibling
);
491 p
= sd
->s_children
.next
;
492 while (n
&& p
!= &sd
->s_children
) {
493 struct sysfs_dirent
*next
;
494 next
= list_entry(p
, struct sysfs_dirent
,
500 list_add_tail(&cursor
->s_sibling
, p
);
503 mutex_unlock(&dentry
->d_inode
->i_mutex
);
507 const struct file_operations sysfs_dir_operations
= {
508 .open
= sysfs_dir_open
,
509 .release
= sysfs_dir_close
,
510 .llseek
= sysfs_dir_lseek
,
511 .read
= generic_read_dir
,
512 .readdir
= sysfs_readdir
,