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