xref: /linux/fs/sysfs/dir.c (revision 94bebf4d1b8e7719f0f3944c037a21cfd99a4af7)
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(struct sysfs_dirent * parent_sd,
37 						void * element)
38 {
39 	struct sysfs_dirent * sd;
40 
41 	sd = kmem_cache_alloc(sysfs_dir_cachep, GFP_KERNEL);
42 	if (!sd)
43 		return NULL;
44 
45 	memset(sd, 0, sizeof(*sd));
46 	atomic_set(&sd->s_count, 1);
47 	atomic_set(&sd->s_event, 1);
48 	INIT_LIST_HEAD(&sd->s_children);
49 	list_add(&sd->s_sibling, &parent_sd->s_children);
50 	sd->s_element = element;
51 
52 	return sd;
53 }
54 
55 /*
56  *
57  * Return -EEXIST if there is already a sysfs element with the same name for
58  * the same parent.
59  *
60  * called with parent inode's i_mutex held
61  */
62 int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
63 			  const unsigned char *new)
64 {
65 	struct sysfs_dirent * sd;
66 
67 	list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
68 		if (sd->s_element) {
69 			const unsigned char *existing = sysfs_get_name(sd);
70 			if (strcmp(existing, new))
71 				continue;
72 			else
73 				return -EEXIST;
74 		}
75 	}
76 
77 	return 0;
78 }
79 
80 
81 int sysfs_make_dirent(struct sysfs_dirent * parent_sd, struct dentry * dentry,
82 			void * element, umode_t mode, int type)
83 {
84 	struct sysfs_dirent * sd;
85 
86 	sd = sysfs_new_dirent(parent_sd, element);
87 	if (!sd)
88 		return -ENOMEM;
89 
90 	sd->s_mode = mode;
91 	sd->s_type = type;
92 	sd->s_dentry = dentry;
93 	if (dentry) {
94 		dentry->d_fsdata = sysfs_get(sd);
95 		dentry->d_op = &sysfs_dentry_ops;
96 	}
97 
98 	return 0;
99 }
100 
101 static int init_dir(struct inode * inode)
102 {
103 	inode->i_op = &sysfs_dir_inode_operations;
104 	inode->i_fop = &sysfs_dir_operations;
105 
106 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
107 	inc_nlink(inode);
108 	return 0;
109 }
110 
111 static int init_file(struct inode * inode)
112 {
113 	inode->i_size = PAGE_SIZE;
114 	inode->i_fop = &sysfs_file_operations;
115 	return 0;
116 }
117 
118 static int init_symlink(struct inode * inode)
119 {
120 	inode->i_op = &sysfs_symlink_inode_operations;
121 	return 0;
122 }
123 
124 static int create_dir(struct kobject * k, struct dentry * p,
125 		      const char * n, struct dentry ** d)
126 {
127 	int error;
128 	umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
129 
130 	mutex_lock(&p->d_inode->i_mutex);
131 	*d = lookup_one_len(n, p, strlen(n));
132 	if (!IS_ERR(*d)) {
133  		if (sysfs_dirent_exist(p->d_fsdata, n))
134   			error = -EEXIST;
135   		else
136 			error = sysfs_make_dirent(p->d_fsdata, *d, k, mode,
137 								SYSFS_DIR);
138 		if (!error) {
139 			error = sysfs_create(*d, mode, init_dir);
140 			if (!error) {
141 				inc_nlink(p->d_inode);
142 				(*d)->d_op = &sysfs_dentry_ops;
143 				d_rehash(*d);
144 			}
145 		}
146 		if (error && (error != -EEXIST)) {
147 			struct sysfs_dirent *sd = (*d)->d_fsdata;
148 			if (sd) {
149  				list_del_init(&sd->s_sibling);
150 				sysfs_put(sd);
151 			}
152 			d_drop(*d);
153 		}
154 		dput(*d);
155 	} else
156 		error = PTR_ERR(*d);
157 	mutex_unlock(&p->d_inode->i_mutex);
158 	return error;
159 }
160 
161 
162 int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
163 {
164 	return create_dir(k,k->dentry,n,d);
165 }
166 
167 /**
168  *	sysfs_create_dir - create a directory for an object.
169  *	@parent:	parent parent object.
170  *	@kobj:		object we're creating directory for.
171  */
172 
173 int sysfs_create_dir(struct kobject * kobj)
174 {
175 	struct dentry * dentry = NULL;
176 	struct dentry * parent;
177 	int error = 0;
178 
179 	BUG_ON(!kobj);
180 
181 	if (kobj->parent)
182 		parent = kobj->parent->dentry;
183 	else if (sysfs_mount && sysfs_mount->mnt_sb)
184 		parent = sysfs_mount->mnt_sb->s_root;
185 	else
186 		return -EFAULT;
187 
188 	error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
189 	if (!error)
190 		kobj->dentry = dentry;
191 	return error;
192 }
193 
194 /* attaches attribute's sysfs_dirent to the dentry corresponding to the
195  * attribute file
196  */
197 static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
198 {
199 	struct attribute * attr = NULL;
200 	struct bin_attribute * bin_attr = NULL;
201 	int (* init) (struct inode *) = NULL;
202 	int error = 0;
203 
204         if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
205                 bin_attr = sd->s_element;
206                 attr = &bin_attr->attr;
207         } else {
208                 attr = sd->s_element;
209                 init = init_file;
210         }
211 
212 	dentry->d_fsdata = sysfs_get(sd);
213 	sd->s_dentry = dentry;
214 	error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
215 	if (error) {
216 		sysfs_put(sd);
217 		return error;
218 	}
219 
220         if (bin_attr) {
221 		dentry->d_inode->i_size = bin_attr->size;
222 		dentry->d_inode->i_fop = &bin_fops;
223 	}
224 	dentry->d_op = &sysfs_dentry_ops;
225 	d_rehash(dentry);
226 
227 	return 0;
228 }
229 
230 static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
231 {
232 	int err = 0;
233 
234 	dentry->d_fsdata = sysfs_get(sd);
235 	sd->s_dentry = dentry;
236 	err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
237 	if (!err) {
238 		dentry->d_op = &sysfs_dentry_ops;
239 		d_rehash(dentry);
240 	} else
241 		sysfs_put(sd);
242 
243 	return err;
244 }
245 
246 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
247 				struct nameidata *nd)
248 {
249 	struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
250 	struct sysfs_dirent * sd;
251 	int err = 0;
252 
253 	list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
254 		if (sd->s_type & SYSFS_NOT_PINNED) {
255 			const unsigned char * name = sysfs_get_name(sd);
256 
257 			if (strcmp(name, dentry->d_name.name))
258 				continue;
259 
260 			if (sd->s_type & SYSFS_KOBJ_LINK)
261 				err = sysfs_attach_link(sd, dentry);
262 			else
263 				err = sysfs_attach_attr(sd, dentry);
264 			break;
265 		}
266 	}
267 
268 	return ERR_PTR(err);
269 }
270 
271 struct inode_operations sysfs_dir_inode_operations = {
272 	.lookup		= sysfs_lookup,
273 	.setattr	= sysfs_setattr,
274 };
275 
276 static void remove_dir(struct dentry * d)
277 {
278 	struct dentry * parent = dget(d->d_parent);
279 	struct sysfs_dirent * sd;
280 
281 	mutex_lock(&parent->d_inode->i_mutex);
282 	d_delete(d);
283 	sd = d->d_fsdata;
284  	list_del_init(&sd->s_sibling);
285 	sysfs_put(sd);
286 	if (d->d_inode)
287 		simple_rmdir(parent->d_inode,d);
288 
289 	pr_debug(" o %s removing done (%d)\n",d->d_name.name,
290 		 atomic_read(&d->d_count));
291 
292 	mutex_unlock(&parent->d_inode->i_mutex);
293 	dput(parent);
294 }
295 
296 void sysfs_remove_subdir(struct dentry * d)
297 {
298 	remove_dir(d);
299 }
300 
301 
302 /**
303  *	sysfs_remove_dir - remove an object's directory.
304  *	@kobj:	object.
305  *
306  *	The only thing special about this is that we remove any files in
307  *	the directory before we remove the directory, and we've inlined
308  *	what used to be sysfs_rmdir() below, instead of calling separately.
309  */
310 
311 void sysfs_remove_dir(struct kobject * kobj)
312 {
313 	struct dentry * dentry = dget(kobj->dentry);
314 	struct sysfs_dirent * parent_sd;
315 	struct sysfs_dirent * sd, * tmp;
316 
317 	if (!dentry)
318 		return;
319 
320 	pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
321 	mutex_lock(&dentry->d_inode->i_mutex);
322 	parent_sd = dentry->d_fsdata;
323 	list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
324 		if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED))
325 			continue;
326 		list_del_init(&sd->s_sibling);
327 		sysfs_drop_dentry(sd, dentry);
328 		sysfs_put(sd);
329 	}
330 	mutex_unlock(&dentry->d_inode->i_mutex);
331 
332 	remove_dir(dentry);
333 	/**
334 	 * Drop reference from dget() on entrance.
335 	 */
336 	dput(dentry);
337 	kobj->dentry = NULL;
338 }
339 
340 int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
341 {
342 	int error = 0;
343 	struct dentry * new_dentry, * parent;
344 
345 	if (!strcmp(kobject_name(kobj), new_name))
346 		return -EINVAL;
347 
348 	if (!kobj->parent)
349 		return -EINVAL;
350 
351 	down_write(&sysfs_rename_sem);
352 	parent = kobj->parent->dentry;
353 
354 	mutex_lock(&parent->d_inode->i_mutex);
355 
356 	new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
357 	if (!IS_ERR(new_dentry)) {
358   		if (!new_dentry->d_inode) {
359 			error = kobject_set_name(kobj, "%s", new_name);
360 			if (!error) {
361 				d_add(new_dentry, NULL);
362 				d_move(kobj->dentry, new_dentry);
363 			}
364 			else
365 				d_drop(new_dentry);
366 		} else
367 			error = -EEXIST;
368 		dput(new_dentry);
369 	}
370 	mutex_unlock(&parent->d_inode->i_mutex);
371 	up_write(&sysfs_rename_sem);
372 
373 	return error;
374 }
375 
376 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
377 {
378 	struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
379 	struct sysfs_dirent *new_parent_sd, *sd;
380 	int error;
381 
382 	old_parent_dentry = kobj->parent ?
383 		kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
384 	new_parent_dentry = new_parent ?
385 		new_parent->dentry : sysfs_mount->mnt_sb->s_root;
386 
387 again:
388 	mutex_lock(&old_parent_dentry->d_inode->i_mutex);
389 	if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
390 		mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
391 		goto again;
392 	}
393 
394 	new_parent_sd = new_parent_dentry->d_fsdata;
395 	sd = kobj->dentry->d_fsdata;
396 
397 	new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
398 				    strlen(kobj->name));
399 	if (IS_ERR(new_dentry)) {
400 		error = PTR_ERR(new_dentry);
401 		goto out;
402 	} else
403 		error = 0;
404 	d_add(new_dentry, NULL);
405 	d_move(kobj->dentry, new_dentry);
406 	dput(new_dentry);
407 
408 	/* Remove from old parent's list and insert into new parent's list. */
409 	list_del_init(&sd->s_sibling);
410 	list_add(&sd->s_sibling, &new_parent_sd->s_children);
411 
412 out:
413 	mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
414 	mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
415 
416 	return error;
417 }
418 
419 static int sysfs_dir_open(struct inode *inode, struct file *file)
420 {
421 	struct dentry * dentry = file->f_path.dentry;
422 	struct sysfs_dirent * parent_sd = dentry->d_fsdata;
423 
424 	mutex_lock(&dentry->d_inode->i_mutex);
425 	file->private_data = sysfs_new_dirent(parent_sd, NULL);
426 	mutex_unlock(&dentry->d_inode->i_mutex);
427 
428 	return file->private_data ? 0 : -ENOMEM;
429 
430 }
431 
432 static int sysfs_dir_close(struct inode *inode, struct file *file)
433 {
434 	struct dentry * dentry = file->f_path.dentry;
435 	struct sysfs_dirent * cursor = file->private_data;
436 
437 	mutex_lock(&dentry->d_inode->i_mutex);
438 	list_del_init(&cursor->s_sibling);
439 	mutex_unlock(&dentry->d_inode->i_mutex);
440 
441 	release_sysfs_dirent(cursor);
442 
443 	return 0;
444 }
445 
446 /* Relationship between s_mode and the DT_xxx types */
447 static inline unsigned char dt_type(struct sysfs_dirent *sd)
448 {
449 	return (sd->s_mode >> 12) & 15;
450 }
451 
452 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
453 {
454 	struct dentry *dentry = filp->f_path.dentry;
455 	struct sysfs_dirent * parent_sd = dentry->d_fsdata;
456 	struct sysfs_dirent *cursor = filp->private_data;
457 	struct list_head *p, *q = &cursor->s_sibling;
458 	ino_t ino;
459 	int i = filp->f_pos;
460 
461 	switch (i) {
462 		case 0:
463 			ino = dentry->d_inode->i_ino;
464 			if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
465 				break;
466 			filp->f_pos++;
467 			i++;
468 			/* fallthrough */
469 		case 1:
470 			ino = parent_ino(dentry);
471 			if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
472 				break;
473 			filp->f_pos++;
474 			i++;
475 			/* fallthrough */
476 		default:
477 			if (filp->f_pos == 2)
478 				list_move(q, &parent_sd->s_children);
479 
480 			for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
481 				struct sysfs_dirent *next;
482 				const char * name;
483 				int len;
484 
485 				next = list_entry(p, struct sysfs_dirent,
486 						   s_sibling);
487 				if (!next->s_element)
488 					continue;
489 
490 				name = sysfs_get_name(next);
491 				len = strlen(name);
492 				if (next->s_dentry)
493 					ino = next->s_dentry->d_inode->i_ino;
494 				else
495 					ino = iunique(sysfs_sb, 2);
496 
497 				if (filldir(dirent, name, len, filp->f_pos, ino,
498 						 dt_type(next)) < 0)
499 					return 0;
500 
501 				list_move(q, p);
502 				p = q;
503 				filp->f_pos++;
504 			}
505 	}
506 	return 0;
507 }
508 
509 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
510 {
511 	struct dentry * dentry = file->f_path.dentry;
512 
513 	mutex_lock(&dentry->d_inode->i_mutex);
514 	switch (origin) {
515 		case 1:
516 			offset += file->f_pos;
517 		case 0:
518 			if (offset >= 0)
519 				break;
520 		default:
521 			mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
522 			return -EINVAL;
523 	}
524 	if (offset != file->f_pos) {
525 		file->f_pos = offset;
526 		if (file->f_pos >= 2) {
527 			struct sysfs_dirent *sd = dentry->d_fsdata;
528 			struct sysfs_dirent *cursor = file->private_data;
529 			struct list_head *p;
530 			loff_t n = file->f_pos - 2;
531 
532 			list_del(&cursor->s_sibling);
533 			p = sd->s_children.next;
534 			while (n && p != &sd->s_children) {
535 				struct sysfs_dirent *next;
536 				next = list_entry(p, struct sysfs_dirent,
537 						   s_sibling);
538 				if (next->s_element)
539 					n--;
540 				p = p->next;
541 			}
542 			list_add_tail(&cursor->s_sibling, p);
543 		}
544 	}
545 	mutex_unlock(&dentry->d_inode->i_mutex);
546 	return offset;
547 }
548 
549 const struct file_operations sysfs_dir_operations = {
550 	.open		= sysfs_dir_open,
551 	.release	= sysfs_dir_close,
552 	.llseek		= sysfs_dir_lseek,
553 	.read		= generic_read_dir,
554 	.readdir	= sysfs_readdir,
555 };
556