2 * dir.c - Operations for sysfs directories.
8 #include <linux/mount.h>
9 #include <linux/module.h>
10 #include <linux/kobject.h>
13 DECLARE_RWSEM(sysfs_rename_sem
);
15 static void sysfs_d_iput(struct dentry
* dentry
, struct inode
* inode
)
17 struct sysfs_dirent
* sd
= dentry
->d_fsdata
;
20 BUG_ON(sd
->s_dentry
!= dentry
);
27 static struct dentry_operations sysfs_dentry_ops
= {
28 .d_iput
= sysfs_d_iput
,
32 * Allocates a new sysfs_dirent and links it to the parent sysfs_dirent
34 static struct sysfs_dirent
* sysfs_new_dirent(struct sysfs_dirent
* parent_sd
,
37 struct sysfs_dirent
* sd
;
39 sd
= kmem_cache_alloc(sysfs_dir_cachep
, GFP_KERNEL
);
43 memset(sd
, 0, sizeof(*sd
));
44 atomic_set(&sd
->s_count
, 1);
45 INIT_LIST_HEAD(&sd
->s_children
);
46 list_add(&sd
->s_sibling
, &parent_sd
->s_children
);
47 sd
->s_element
= element
;
52 int sysfs_make_dirent(struct sysfs_dirent
* parent_sd
, struct dentry
* dentry
,
53 void * element
, umode_t mode
, int type
)
55 struct sysfs_dirent
* sd
;
57 sd
= sysfs_new_dirent(parent_sd
, element
);
63 sd
->s_dentry
= dentry
;
65 dentry
->d_fsdata
= sysfs_get(sd
);
66 dentry
->d_op
= &sysfs_dentry_ops
;
72 static int init_dir(struct inode
* inode
)
74 inode
->i_op
= &sysfs_dir_inode_operations
;
75 inode
->i_fop
= &sysfs_dir_operations
;
77 /* directory inodes start off with i_nlink == 2 (for "." entry) */
82 static int init_file(struct inode
* inode
)
84 inode
->i_size
= PAGE_SIZE
;
85 inode
->i_fop
= &sysfs_file_operations
;
89 static int init_symlink(struct inode
* inode
)
91 inode
->i_op
= &sysfs_symlink_inode_operations
;
95 static int create_dir(struct kobject
* k
, struct dentry
* p
,
96 const char * n
, struct dentry
** d
)
99 umode_t mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
101 down(&p
->d_inode
->i_sem
);
102 *d
= sysfs_get_dentry(p
,n
);
104 error
= sysfs_create(*d
, mode
, init_dir
);
106 error
= sysfs_make_dirent(p
->d_fsdata
, *d
, k
, mode
,
109 p
->d_inode
->i_nlink
++;
110 (*d
)->d_op
= &sysfs_dentry_ops
;
114 if (error
&& (error
!= -EEXIST
))
119 up(&p
->d_inode
->i_sem
);
124 int sysfs_create_subdir(struct kobject
* k
, const char * n
, struct dentry
** d
)
126 return create_dir(k
,k
->dentry
,n
,d
);
130 * sysfs_create_dir - create a directory for an object.
131 * @parent: parent parent object.
132 * @kobj: object we're creating directory for.
135 int sysfs_create_dir(struct kobject
* kobj
)
137 struct dentry
* dentry
= NULL
;
138 struct dentry
* parent
;
144 parent
= kobj
->parent
->dentry
;
145 else if (sysfs_mount
&& sysfs_mount
->mnt_sb
)
146 parent
= sysfs_mount
->mnt_sb
->s_root
;
150 error
= create_dir(kobj
,parent
,kobject_name(kobj
),&dentry
);
152 kobj
->dentry
= dentry
;
156 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
159 static int sysfs_attach_attr(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
161 struct attribute
* attr
= NULL
;
162 struct bin_attribute
* bin_attr
= NULL
;
163 int (* init
) (struct inode
*) = NULL
;
166 if (sd
->s_type
& SYSFS_KOBJ_BIN_ATTR
) {
167 bin_attr
= sd
->s_element
;
168 attr
= &bin_attr
->attr
;
170 attr
= sd
->s_element
;
174 error
= sysfs_create(dentry
, (attr
->mode
& S_IALLUGO
) | S_IFREG
, init
);
179 dentry
->d_inode
->i_size
= bin_attr
->size
;
180 dentry
->d_inode
->i_fop
= &bin_fops
;
182 dentry
->d_op
= &sysfs_dentry_ops
;
183 dentry
->d_fsdata
= sysfs_get(sd
);
184 sd
->s_dentry
= dentry
;
190 static int sysfs_attach_link(struct sysfs_dirent
* sd
, struct dentry
* dentry
)
194 err
= sysfs_create(dentry
, S_IFLNK
|S_IRWXUGO
, init_symlink
);
196 dentry
->d_op
= &sysfs_dentry_ops
;
197 dentry
->d_fsdata
= sysfs_get(sd
);
198 sd
->s_dentry
= dentry
;
204 static struct dentry
* sysfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
205 struct nameidata
*nd
)
207 struct sysfs_dirent
* parent_sd
= dentry
->d_parent
->d_fsdata
;
208 struct sysfs_dirent
* sd
;
211 list_for_each_entry(sd
, &parent_sd
->s_children
, s_sibling
) {
212 if (sd
->s_type
& SYSFS_NOT_PINNED
) {
213 const unsigned char * name
= sysfs_get_name(sd
);
215 if (strcmp(name
, dentry
->d_name
.name
))
218 if (sd
->s_type
& SYSFS_KOBJ_LINK
)
219 err
= sysfs_attach_link(sd
, dentry
);
221 err
= sysfs_attach_attr(sd
, dentry
);
229 struct inode_operations sysfs_dir_inode_operations
= {
230 .lookup
= sysfs_lookup
,
233 static void remove_dir(struct dentry
* d
)
235 struct dentry
* parent
= dget(d
->d_parent
);
236 struct sysfs_dirent
* sd
;
238 down(&parent
->d_inode
->i_sem
);
241 list_del_init(&sd
->s_sibling
);
244 simple_rmdir(parent
->d_inode
,d
);
246 pr_debug(" o %s removing done (%d)\n",d
->d_name
.name
,
247 atomic_read(&d
->d_count
));
249 up(&parent
->d_inode
->i_sem
);
253 void sysfs_remove_subdir(struct dentry
* d
)
260 * sysfs_remove_dir - remove an object's directory.
263 * The only thing special about this is that we remove any files in
264 * the directory before we remove the directory, and we've inlined
265 * what used to be sysfs_rmdir() below, instead of calling separately.
268 void sysfs_remove_dir(struct kobject
* kobj
)
270 struct dentry
* dentry
= dget(kobj
->dentry
);
271 struct sysfs_dirent
* parent_sd
;
272 struct sysfs_dirent
* sd
, * tmp
;
277 pr_debug("sysfs %s: removing dir\n",dentry
->d_name
.name
);
278 down(&dentry
->d_inode
->i_sem
);
279 parent_sd
= dentry
->d_fsdata
;
280 list_for_each_entry_safe(sd
, tmp
, &parent_sd
->s_children
, s_sibling
) {
281 if (!sd
->s_element
|| !(sd
->s_type
& SYSFS_NOT_PINNED
))
283 list_del_init(&sd
->s_sibling
);
284 sysfs_drop_dentry(sd
, dentry
);
287 up(&dentry
->d_inode
->i_sem
);
291 * Drop reference from dget() on entrance.
296 int sysfs_rename_dir(struct kobject
* kobj
, const char *new_name
)
299 struct dentry
* new_dentry
, * parent
;
301 if (!strcmp(kobject_name(kobj
), new_name
))
307 down_write(&sysfs_rename_sem
);
308 parent
= kobj
->parent
->dentry
;
310 down(&parent
->d_inode
->i_sem
);
312 new_dentry
= sysfs_get_dentry(parent
, new_name
);
313 if (!IS_ERR(new_dentry
)) {
314 if (!new_dentry
->d_inode
) {
315 error
= kobject_set_name(kobj
, "%s", new_name
);
317 d_add(new_dentry
, NULL
);
318 d_move(kobj
->dentry
, new_dentry
);
326 up(&parent
->d_inode
->i_sem
);
327 up_write(&sysfs_rename_sem
);
332 static int sysfs_dir_open(struct inode
*inode
, struct file
*file
)
334 struct dentry
* dentry
= file
->f_dentry
;
335 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
337 down(&dentry
->d_inode
->i_sem
);
338 file
->private_data
= sysfs_new_dirent(parent_sd
, NULL
);
339 up(&dentry
->d_inode
->i_sem
);
341 return file
->private_data
? 0 : -ENOMEM
;
345 static int sysfs_dir_close(struct inode
*inode
, struct file
*file
)
347 struct dentry
* dentry
= file
->f_dentry
;
348 struct sysfs_dirent
* cursor
= file
->private_data
;
350 down(&dentry
->d_inode
->i_sem
);
351 list_del_init(&cursor
->s_sibling
);
352 up(&dentry
->d_inode
->i_sem
);
354 release_sysfs_dirent(cursor
);
359 /* Relationship between s_mode and the DT_xxx types */
360 static inline unsigned char dt_type(struct sysfs_dirent
*sd
)
362 return (sd
->s_mode
>> 12) & 15;
365 static int sysfs_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
367 struct dentry
*dentry
= filp
->f_dentry
;
368 struct sysfs_dirent
* parent_sd
= dentry
->d_fsdata
;
369 struct sysfs_dirent
*cursor
= filp
->private_data
;
370 struct list_head
*p
, *q
= &cursor
->s_sibling
;
376 ino
= dentry
->d_inode
->i_ino
;
377 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
383 ino
= parent_ino(dentry
);
384 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
390 if (filp
->f_pos
== 2) {
392 list_add(q
, &parent_sd
->s_children
);
394 for (p
=q
->next
; p
!= &parent_sd
->s_children
; p
=p
->next
) {
395 struct sysfs_dirent
*next
;
399 next
= list_entry(p
, struct sysfs_dirent
,
401 if (!next
->s_element
)
404 name
= sysfs_get_name(next
);
407 ino
= next
->s_dentry
->d_inode
->i_ino
;
409 ino
= iunique(sysfs_sb
, 2);
411 if (filldir(dirent
, name
, len
, filp
->f_pos
, ino
,
424 static loff_t
sysfs_dir_lseek(struct file
* file
, loff_t offset
, int origin
)
426 struct dentry
* dentry
= file
->f_dentry
;
428 down(&dentry
->d_inode
->i_sem
);
431 offset
+= file
->f_pos
;
436 up(&file
->f_dentry
->d_inode
->i_sem
);
439 if (offset
!= file
->f_pos
) {
440 file
->f_pos
= offset
;
441 if (file
->f_pos
>= 2) {
442 struct sysfs_dirent
*sd
= dentry
->d_fsdata
;
443 struct sysfs_dirent
*cursor
= file
->private_data
;
445 loff_t n
= file
->f_pos
- 2;
447 list_del(&cursor
->s_sibling
);
448 p
= sd
->s_children
.next
;
449 while (n
&& p
!= &sd
->s_children
) {
450 struct sysfs_dirent
*next
;
451 next
= list_entry(p
, struct sysfs_dirent
,
457 list_add_tail(&cursor
->s_sibling
, p
);
460 up(&dentry
->d_inode
->i_sem
);
464 struct file_operations sysfs_dir_operations
= {
465 .open
= sysfs_dir_open
,
466 .release
= sysfs_dir_close
,
467 .llseek
= sysfs_dir_lseek
,
468 .read
= generic_read_dir
,
469 .readdir
= sysfs_readdir
,
472 EXPORT_SYMBOL_GPL(sysfs_create_dir
);
473 EXPORT_SYMBOL_GPL(sysfs_remove_dir
);
474 EXPORT_SYMBOL_GPL(sysfs_rename_dir
);