xref: /linux/fs/sysfs/dir.c (revision 8619f979898397582e366877fd5feeba7560d70c)
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 <linux/idr.h>
13 #include <linux/completion.h>
14 #include <asm/semaphore.h>
15 #include "sysfs.h"
16 
17 DECLARE_RWSEM(sysfs_rename_sem);
18 spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED;
19 spinlock_t kobj_sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
20 
21 static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
22 static DEFINE_IDA(sysfs_ino_ida);
23 
24 /**
25  *	sysfs_get_active - get an active reference to sysfs_dirent
26  *	@sd: sysfs_dirent to get an active reference to
27  *
28  *	Get an active reference of @sd.  This function is noop if @sd
29  *	is NULL.
30  *
31  *	RETURNS:
32  *	Pointer to @sd on success, NULL on failure.
33  */
34 struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
35 {
36 	if (unlikely(!sd))
37 		return NULL;
38 
39 	while (1) {
40 		int v, t;
41 
42 		v = atomic_read(&sd->s_active);
43 		if (unlikely(v < 0))
44 			return NULL;
45 
46 		t = atomic_cmpxchg(&sd->s_active, v, v + 1);
47 		if (likely(t == v))
48 			return sd;
49 		if (t < 0)
50 			return NULL;
51 
52 		cpu_relax();
53 	}
54 }
55 
56 /**
57  *	sysfs_put_active - put an active reference to sysfs_dirent
58  *	@sd: sysfs_dirent to put an active reference to
59  *
60  *	Put an active reference to @sd.  This function is noop if @sd
61  *	is NULL.
62  */
63 void sysfs_put_active(struct sysfs_dirent *sd)
64 {
65 	struct completion *cmpl;
66 	int v;
67 
68 	if (unlikely(!sd))
69 		return;
70 
71 	v = atomic_dec_return(&sd->s_active);
72 	if (likely(v != SD_DEACTIVATED_BIAS))
73 		return;
74 
75 	/* atomic_dec_return() is a mb(), we'll always see the updated
76 	 * sd->s_sibling.next.
77 	 */
78 	cmpl = (void *)sd->s_sibling.next;
79 	complete(cmpl);
80 }
81 
82 /**
83  *	sysfs_get_active_two - get active references to sysfs_dirent and parent
84  *	@sd: sysfs_dirent of interest
85  *
86  *	Get active reference to @sd and its parent.  Parent's active
87  *	reference is grabbed first.  This function is noop if @sd is
88  *	NULL.
89  *
90  *	RETURNS:
91  *	Pointer to @sd on success, NULL on failure.
92  */
93 struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
94 {
95 	if (sd) {
96 		if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
97 			return NULL;
98 		if (unlikely(!sysfs_get_active(sd))) {
99 			sysfs_put_active(sd->s_parent);
100 			return NULL;
101 		}
102 	}
103 	return sd;
104 }
105 
106 /**
107  *	sysfs_put_active_two - put active references to sysfs_dirent and parent
108  *	@sd: sysfs_dirent of interest
109  *
110  *	Put active references to @sd and its parent.  This function is
111  *	noop if @sd is NULL.
112  */
113 void sysfs_put_active_two(struct sysfs_dirent *sd)
114 {
115 	if (sd) {
116 		sysfs_put_active(sd);
117 		sysfs_put_active(sd->s_parent);
118 	}
119 }
120 
121 /**
122  *	sysfs_deactivate - deactivate sysfs_dirent
123  *	@sd: sysfs_dirent to deactivate
124  *
125  *	Deny new active references and drain existing ones.
126  */
127 void sysfs_deactivate(struct sysfs_dirent *sd)
128 {
129 	DECLARE_COMPLETION_ONSTACK(wait);
130 	int v;
131 
132 	BUG_ON(!list_empty(&sd->s_sibling));
133 	sd->s_sibling.next = (void *)&wait;
134 
135 	/* atomic_add_return() is a mb(), put_active() will always see
136 	 * the updated sd->s_sibling.next.
137 	 */
138 	v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
139 
140 	if (v != SD_DEACTIVATED_BIAS)
141 		wait_for_completion(&wait);
142 
143 	INIT_LIST_HEAD(&sd->s_sibling);
144 }
145 
146 static int sysfs_alloc_ino(ino_t *pino)
147 {
148 	int ino, rc;
149 
150  retry:
151 	spin_lock(&sysfs_ino_lock);
152 	rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
153 	spin_unlock(&sysfs_ino_lock);
154 
155 	if (rc == -EAGAIN) {
156 		if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
157 			goto retry;
158 		rc = -ENOMEM;
159 	}
160 
161 	*pino = ino;
162 	return rc;
163 }
164 
165 static void sysfs_free_ino(ino_t ino)
166 {
167 	spin_lock(&sysfs_ino_lock);
168 	ida_remove(&sysfs_ino_ida, ino);
169 	spin_unlock(&sysfs_ino_lock);
170 }
171 
172 void release_sysfs_dirent(struct sysfs_dirent * sd)
173 {
174 	struct sysfs_dirent *parent_sd;
175 
176  repeat:
177 	parent_sd = sd->s_parent;
178 
179 	if (sd->s_type & SYSFS_KOBJ_LINK)
180 		sysfs_put(sd->s_elem.symlink.target_sd);
181 	if (sd->s_type & SYSFS_COPY_NAME)
182 		kfree(sd->s_name);
183 	kfree(sd->s_iattr);
184 	sysfs_free_ino(sd->s_ino);
185 	kmem_cache_free(sysfs_dir_cachep, sd);
186 
187 	sd = parent_sd;
188 	if (sd && atomic_dec_and_test(&sd->s_count))
189 		goto repeat;
190 }
191 
192 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
193 {
194 	struct sysfs_dirent * sd = dentry->d_fsdata;
195 
196 	if (sd) {
197 		/* sd->s_dentry is protected with sysfs_lock.  This
198 		 * allows sysfs_drop_dentry() to dereference it.
199 		 */
200 		spin_lock(&sysfs_lock);
201 
202 		/* The dentry might have been deleted or another
203 		 * lookup could have happened updating sd->s_dentry to
204 		 * point the new dentry.  Ignore if it isn't pointing
205 		 * to this dentry.
206 		 */
207 		if (sd->s_dentry == dentry)
208 			sd->s_dentry = NULL;
209 		spin_unlock(&sysfs_lock);
210 		sysfs_put(sd);
211 	}
212 	iput(inode);
213 }
214 
215 static struct dentry_operations sysfs_dentry_ops = {
216 	.d_iput		= sysfs_d_iput,
217 };
218 
219 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
220 {
221 	char *dup_name = NULL;
222 	struct sysfs_dirent *sd = NULL;
223 
224 	if (type & SYSFS_COPY_NAME) {
225 		name = dup_name = kstrdup(name, GFP_KERNEL);
226 		if (!name)
227 			goto err_out;
228 	}
229 
230 	sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
231 	if (!sd)
232 		goto err_out;
233 
234 	if (sysfs_alloc_ino(&sd->s_ino))
235 		goto err_out;
236 
237 	atomic_set(&sd->s_count, 1);
238 	atomic_set(&sd->s_active, 0);
239 	atomic_set(&sd->s_event, 1);
240 	INIT_LIST_HEAD(&sd->s_children);
241 	INIT_LIST_HEAD(&sd->s_sibling);
242 
243 	sd->s_name = name;
244 	sd->s_mode = mode;
245 	sd->s_type = type;
246 
247 	return sd;
248 
249  err_out:
250 	kfree(dup_name);
251 	kmem_cache_free(sysfs_dir_cachep, sd);
252 	return NULL;
253 }
254 
255 static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
256 {
257 	dentry->d_op = &sysfs_dentry_ops;
258 	dentry->d_fsdata = sysfs_get(sd);
259 
260 	/* protect sd->s_dentry against sysfs_d_iput */
261 	spin_lock(&sysfs_lock);
262 	sd->s_dentry = dentry;
263 	spin_unlock(&sysfs_lock);
264 
265 	d_rehash(dentry);
266 }
267 
268 void sysfs_attach_dirent(struct sysfs_dirent *sd,
269 			 struct sysfs_dirent *parent_sd, struct dentry *dentry)
270 {
271 	if (dentry)
272 		sysfs_attach_dentry(sd, dentry);
273 
274 	if (parent_sd) {
275 		sd->s_parent = sysfs_get(parent_sd);
276 		list_add(&sd->s_sibling, &parent_sd->s_children);
277 	}
278 }
279 
280 /*
281  *
282  * Return -EEXIST if there is already a sysfs element with the same name for
283  * the same parent.
284  *
285  * called with parent inode's i_mutex held
286  */
287 int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
288 			  const unsigned char *new)
289 {
290 	struct sysfs_dirent * sd;
291 
292 	list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
293 		if (sd->s_type) {
294 			if (strcmp(sd->s_name, new))
295 				continue;
296 			else
297 				return -EEXIST;
298 		}
299 	}
300 
301 	return 0;
302 }
303 
304 static int create_dir(struct kobject *kobj, struct dentry *parent,
305 		      const char *name, struct dentry **p_dentry)
306 {
307 	int error;
308 	umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
309 	struct dentry *dentry;
310 	struct inode *inode;
311 	struct sysfs_dirent *sd;
312 
313 	mutex_lock(&parent->d_inode->i_mutex);
314 
315 	/* allocate */
316 	dentry = lookup_one_len(name, parent, strlen(name));
317 	if (IS_ERR(dentry)) {
318 		error = PTR_ERR(dentry);
319 		goto out_unlock;
320 	}
321 
322 	error = -EEXIST;
323 	if (dentry->d_inode)
324 		goto out_dput;
325 
326 	error = -ENOMEM;
327 	sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
328 	if (!sd)
329 		goto out_drop;
330 	sd->s_elem.dir.kobj = kobj;
331 
332 	inode = sysfs_get_inode(sd);
333 	if (!inode)
334 		goto out_sput;
335 
336 	if (inode->i_state & I_NEW) {
337 		inode->i_op = &sysfs_dir_inode_operations;
338 		inode->i_fop = &sysfs_dir_operations;
339 		/* directory inodes start off with i_nlink == 2 (for ".") */
340 		inc_nlink(inode);
341 	}
342 
343 	/* link in */
344 	error = -EEXIST;
345 	if (sysfs_dirent_exist(parent->d_fsdata, name))
346 		goto out_iput;
347 
348 	sysfs_instantiate(dentry, inode);
349 	inc_nlink(parent->d_inode);
350 	sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
351 
352 	*p_dentry = dentry;
353 	error = 0;
354 	goto out_unlock;	/* pin directory dentry in core */
355 
356  out_iput:
357 	iput(inode);
358  out_sput:
359 	sysfs_put(sd);
360  out_drop:
361 	d_drop(dentry);
362  out_dput:
363 	dput(dentry);
364  out_unlock:
365 	mutex_unlock(&parent->d_inode->i_mutex);
366 	return error;
367 }
368 
369 
370 int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
371 {
372 	return create_dir(k,k->dentry,n,d);
373 }
374 
375 /**
376  *	sysfs_create_dir - create a directory for an object.
377  *	@kobj:		object we're creating directory for.
378  *	@shadow_parent:	parent parent object.
379  */
380 
381 int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
382 {
383 	struct dentry * dentry = NULL;
384 	struct dentry * parent;
385 	int error = 0;
386 
387 	BUG_ON(!kobj);
388 
389 	if (shadow_parent)
390 		parent = shadow_parent;
391 	else if (kobj->parent)
392 		parent = kobj->parent->dentry;
393 	else if (sysfs_mount && sysfs_mount->mnt_sb)
394 		parent = sysfs_mount->mnt_sb->s_root;
395 	else
396 		return -EFAULT;
397 
398 	error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
399 	if (!error)
400 		kobj->dentry = dentry;
401 	return error;
402 }
403 
404 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
405 				struct nameidata *nd)
406 {
407 	struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
408 	struct sysfs_dirent * sd;
409 	struct inode *inode;
410 	int found = 0;
411 
412 	list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
413 		if ((sd->s_type & SYSFS_NOT_PINNED) &&
414 		    !strcmp(sd->s_name, dentry->d_name.name)) {
415 			found = 1;
416 			break;
417 		}
418 	}
419 
420 	/* no such entry */
421 	if (!found)
422 		return NULL;
423 
424 	/* attach dentry and inode */
425 	inode = sysfs_get_inode(sd);
426 	if (!inode)
427 		return ERR_PTR(-ENOMEM);
428 
429 	if (inode->i_state & I_NEW) {
430 		/* initialize inode according to type */
431 		if (sd->s_type & SYSFS_KOBJ_ATTR) {
432 			inode->i_size = PAGE_SIZE;
433 			inode->i_fop = &sysfs_file_operations;
434 		} else if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
435 			struct bin_attribute *bin_attr =
436 				sd->s_elem.bin_attr.bin_attr;
437 			inode->i_size = bin_attr->size;
438 			inode->i_fop = &bin_fops;
439 		} else if (sd->s_type & SYSFS_KOBJ_LINK)
440 			inode->i_op = &sysfs_symlink_inode_operations;
441 	}
442 
443 	sysfs_instantiate(dentry, inode);
444 	sysfs_attach_dentry(sd, dentry);
445 
446 	return NULL;
447 }
448 
449 const struct inode_operations sysfs_dir_inode_operations = {
450 	.lookup		= sysfs_lookup,
451 	.setattr	= sysfs_setattr,
452 };
453 
454 static void remove_dir(struct dentry * d)
455 {
456 	struct dentry *parent = d->d_parent;
457 	struct sysfs_dirent *sd = d->d_fsdata;
458 
459 	mutex_lock(&parent->d_inode->i_mutex);
460 
461  	list_del_init(&sd->s_sibling);
462 
463 	pr_debug(" o %s removing done (%d)\n",d->d_name.name,
464 		 atomic_read(&d->d_count));
465 
466 	mutex_unlock(&parent->d_inode->i_mutex);
467 
468 	sysfs_drop_dentry(sd);
469 	sysfs_deactivate(sd);
470 	sysfs_put(sd);
471 }
472 
473 void sysfs_remove_subdir(struct dentry * d)
474 {
475 	remove_dir(d);
476 }
477 
478 
479 static void __sysfs_remove_dir(struct dentry *dentry)
480 {
481 	LIST_HEAD(removed);
482 	struct sysfs_dirent * parent_sd;
483 	struct sysfs_dirent * sd, * tmp;
484 
485 	if (!dentry)
486 		return;
487 
488 	pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
489 	mutex_lock(&dentry->d_inode->i_mutex);
490 	parent_sd = dentry->d_fsdata;
491 	list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
492 		if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED))
493 			continue;
494 		list_move(&sd->s_sibling, &removed);
495 	}
496 	mutex_unlock(&dentry->d_inode->i_mutex);
497 
498 	list_for_each_entry_safe(sd, tmp, &removed, s_sibling) {
499 		list_del_init(&sd->s_sibling);
500 		sysfs_drop_dentry(sd);
501 		sysfs_deactivate(sd);
502 		sysfs_put(sd);
503 	}
504 
505 	remove_dir(dentry);
506 }
507 
508 /**
509  *	sysfs_remove_dir - remove an object's directory.
510  *	@kobj:	object.
511  *
512  *	The only thing special about this is that we remove any files in
513  *	the directory before we remove the directory, and we've inlined
514  *	what used to be sysfs_rmdir() below, instead of calling separately.
515  */
516 
517 void sysfs_remove_dir(struct kobject * kobj)
518 {
519 	struct dentry *d = kobj->dentry;
520 
521 	spin_lock(&kobj_sysfs_assoc_lock);
522 	kobj->dentry = NULL;
523 	spin_unlock(&kobj_sysfs_assoc_lock);
524 
525 	__sysfs_remove_dir(d);
526 }
527 
528 int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
529 		     const char *new_name)
530 {
531 	struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
532 	struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
533 	struct dentry *new_dentry;
534 	char *dup_name;
535 	int error;
536 
537 	if (!new_parent)
538 		return -EFAULT;
539 
540 	down_write(&sysfs_rename_sem);
541 	mutex_lock(&new_parent->d_inode->i_mutex);
542 
543 	new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
544 	if (IS_ERR(new_dentry)) {
545 		error = PTR_ERR(new_dentry);
546 		goto out_unlock;
547 	}
548 
549 	/* By allowing two different directories with the same
550 	 * d_parent we allow this routine to move between different
551 	 * shadows of the same directory
552 	 */
553 	error = -EINVAL;
554 	if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
555 	    new_dentry->d_parent->d_inode != new_parent->d_inode ||
556 	    new_dentry == kobj->dentry)
557 		goto out_dput;
558 
559 	error = -EEXIST;
560 	if (new_dentry->d_inode)
561 		goto out_dput;
562 
563 	/* rename kobject and sysfs_dirent */
564 	error = -ENOMEM;
565 	new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
566 	if (!new_name)
567 		goto out_drop;
568 
569 	error = kobject_set_name(kobj, "%s", new_name);
570 	if (error)
571 		goto out_free;
572 
573 	kfree(sd->s_name);
574 	sd->s_name = new_name;
575 
576 	/* move under the new parent */
577 	d_add(new_dentry, NULL);
578 	d_move(kobj->dentry, new_dentry);
579 
580 	list_del_init(&sd->s_sibling);
581 	sysfs_get(parent_sd);
582 	sysfs_put(sd->s_parent);
583 	sd->s_parent = parent_sd;
584 	list_add(&sd->s_sibling, &parent_sd->s_children);
585 
586 	error = 0;
587 	goto out_unlock;
588 
589  out_free:
590 	kfree(dup_name);
591  out_drop:
592 	d_drop(new_dentry);
593  out_dput:
594 	dput(new_dentry);
595  out_unlock:
596 	mutex_unlock(&new_parent->d_inode->i_mutex);
597 	up_write(&sysfs_rename_sem);
598 	return error;
599 }
600 
601 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
602 {
603 	struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
604 	struct sysfs_dirent *new_parent_sd, *sd;
605 	int error;
606 
607 	old_parent_dentry = kobj->parent ?
608 		kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
609 	new_parent_dentry = new_parent ?
610 		new_parent->dentry : sysfs_mount->mnt_sb->s_root;
611 
612 	if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
613 		return 0;	/* nothing to move */
614 again:
615 	mutex_lock(&old_parent_dentry->d_inode->i_mutex);
616 	if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
617 		mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
618 		goto again;
619 	}
620 
621 	new_parent_sd = new_parent_dentry->d_fsdata;
622 	sd = kobj->dentry->d_fsdata;
623 
624 	new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
625 				    strlen(kobj->name));
626 	if (IS_ERR(new_dentry)) {
627 		error = PTR_ERR(new_dentry);
628 		goto out;
629 	} else
630 		error = 0;
631 	d_add(new_dentry, NULL);
632 	d_move(kobj->dentry, new_dentry);
633 	dput(new_dentry);
634 
635 	/* Remove from old parent's list and insert into new parent's list. */
636 	list_del_init(&sd->s_sibling);
637 	sysfs_get(new_parent_sd);
638 	sysfs_put(sd->s_parent);
639 	sd->s_parent = new_parent_sd;
640 	list_add(&sd->s_sibling, &new_parent_sd->s_children);
641 
642 out:
643 	mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
644 	mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
645 
646 	return error;
647 }
648 
649 static int sysfs_dir_open(struct inode *inode, struct file *file)
650 {
651 	struct dentry * dentry = file->f_path.dentry;
652 	struct sysfs_dirent * parent_sd = dentry->d_fsdata;
653 	struct sysfs_dirent * sd;
654 
655 	mutex_lock(&dentry->d_inode->i_mutex);
656 	sd = sysfs_new_dirent("_DIR_", 0, 0);
657 	if (sd)
658 		sysfs_attach_dirent(sd, parent_sd, NULL);
659 	mutex_unlock(&dentry->d_inode->i_mutex);
660 
661 	file->private_data = sd;
662 	return sd ? 0 : -ENOMEM;
663 }
664 
665 static int sysfs_dir_close(struct inode *inode, struct file *file)
666 {
667 	struct dentry * dentry = file->f_path.dentry;
668 	struct sysfs_dirent * cursor = file->private_data;
669 
670 	mutex_lock(&dentry->d_inode->i_mutex);
671 	list_del_init(&cursor->s_sibling);
672 	mutex_unlock(&dentry->d_inode->i_mutex);
673 
674 	release_sysfs_dirent(cursor);
675 
676 	return 0;
677 }
678 
679 /* Relationship between s_mode and the DT_xxx types */
680 static inline unsigned char dt_type(struct sysfs_dirent *sd)
681 {
682 	return (sd->s_mode >> 12) & 15;
683 }
684 
685 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
686 {
687 	struct dentry *dentry = filp->f_path.dentry;
688 	struct sysfs_dirent * parent_sd = dentry->d_fsdata;
689 	struct sysfs_dirent *cursor = filp->private_data;
690 	struct list_head *p, *q = &cursor->s_sibling;
691 	ino_t ino;
692 	int i = filp->f_pos;
693 
694 	switch (i) {
695 		case 0:
696 			ino = parent_sd->s_ino;
697 			if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
698 				break;
699 			filp->f_pos++;
700 			i++;
701 			/* fallthrough */
702 		case 1:
703 			if (parent_sd->s_parent)
704 				ino = parent_sd->s_parent->s_ino;
705 			else
706 				ino = parent_sd->s_ino;
707 			if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
708 				break;
709 			filp->f_pos++;
710 			i++;
711 			/* fallthrough */
712 		default:
713 			if (filp->f_pos == 2)
714 				list_move(q, &parent_sd->s_children);
715 
716 			for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
717 				struct sysfs_dirent *next;
718 				const char * name;
719 				int len;
720 
721 				next = list_entry(p, struct sysfs_dirent,
722 						   s_sibling);
723 				if (!next->s_type)
724 					continue;
725 
726 				name = next->s_name;
727 				len = strlen(name);
728 				ino = next->s_ino;
729 
730 				if (filldir(dirent, name, len, filp->f_pos, ino,
731 						 dt_type(next)) < 0)
732 					return 0;
733 
734 				list_move(q, p);
735 				p = q;
736 				filp->f_pos++;
737 			}
738 	}
739 	return 0;
740 }
741 
742 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
743 {
744 	struct dentry * dentry = file->f_path.dentry;
745 
746 	mutex_lock(&dentry->d_inode->i_mutex);
747 	switch (origin) {
748 		case 1:
749 			offset += file->f_pos;
750 		case 0:
751 			if (offset >= 0)
752 				break;
753 		default:
754 			mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
755 			return -EINVAL;
756 	}
757 	if (offset != file->f_pos) {
758 		file->f_pos = offset;
759 		if (file->f_pos >= 2) {
760 			struct sysfs_dirent *sd = dentry->d_fsdata;
761 			struct sysfs_dirent *cursor = file->private_data;
762 			struct list_head *p;
763 			loff_t n = file->f_pos - 2;
764 
765 			list_del(&cursor->s_sibling);
766 			p = sd->s_children.next;
767 			while (n && p != &sd->s_children) {
768 				struct sysfs_dirent *next;
769 				next = list_entry(p, struct sysfs_dirent,
770 						   s_sibling);
771 				if (next->s_type)
772 					n--;
773 				p = p->next;
774 			}
775 			list_add_tail(&cursor->s_sibling, p);
776 		}
777 	}
778 	mutex_unlock(&dentry->d_inode->i_mutex);
779 	return offset;
780 }
781 
782 
783 /**
784  *	sysfs_make_shadowed_dir - Setup so a directory can be shadowed
785  *	@kobj:	object we're creating shadow of.
786  */
787 
788 int sysfs_make_shadowed_dir(struct kobject *kobj,
789 	void * (*follow_link)(struct dentry *, struct nameidata *))
790 {
791 	struct inode *inode;
792 	struct inode_operations *i_op;
793 
794 	inode = kobj->dentry->d_inode;
795 	if (inode->i_op != &sysfs_dir_inode_operations)
796 		return -EINVAL;
797 
798 	i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
799 	if (!i_op)
800 		return -ENOMEM;
801 
802 	memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
803 	i_op->follow_link = follow_link;
804 
805 	/* Locking of inode->i_op?
806 	 * Since setting i_op is a single word write and they
807 	 * are atomic we should be ok here.
808 	 */
809 	inode->i_op = i_op;
810 	return 0;
811 }
812 
813 /**
814  *	sysfs_create_shadow_dir - create a shadow directory for an object.
815  *	@kobj:	object we're creating directory for.
816  *
817  *	sysfs_make_shadowed_dir must already have been called on this
818  *	directory.
819  */
820 
821 struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
822 {
823 	struct dentry *dir = kobj->dentry;
824 	struct inode *inode = dir->d_inode;
825 	struct dentry *parent = dir->d_parent;
826 	struct sysfs_dirent *parent_sd = parent->d_fsdata;
827 	struct dentry *shadow;
828 	struct sysfs_dirent *sd;
829 
830 	shadow = ERR_PTR(-EINVAL);
831 	if (!sysfs_is_shadowed_inode(inode))
832 		goto out;
833 
834 	shadow = d_alloc(parent, &dir->d_name);
835 	if (!shadow)
836 		goto nomem;
837 
838 	sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
839 	if (!sd)
840 		goto nomem;
841 	sd->s_elem.dir.kobj = kobj;
842 	/* point to parent_sd but don't attach to it */
843 	sd->s_parent = sysfs_get(parent_sd);
844 	sysfs_attach_dirent(sd, NULL, shadow);
845 
846 	d_instantiate(shadow, igrab(inode));
847 	inc_nlink(inode);
848 	inc_nlink(parent->d_inode);
849 
850 	dget(shadow);		/* Extra count - pin the dentry in core */
851 
852 out:
853 	return shadow;
854 nomem:
855 	dput(shadow);
856 	shadow = ERR_PTR(-ENOMEM);
857 	goto out;
858 }
859 
860 /**
861  *	sysfs_remove_shadow_dir - remove an object's directory.
862  *	@shadow: dentry of shadow directory
863  *
864  *	The only thing special about this is that we remove any files in
865  *	the directory before we remove the directory, and we've inlined
866  *	what used to be sysfs_rmdir() below, instead of calling separately.
867  */
868 
869 void sysfs_remove_shadow_dir(struct dentry *shadow)
870 {
871 	__sysfs_remove_dir(shadow);
872 }
873 
874 const struct file_operations sysfs_dir_operations = {
875 	.open		= sysfs_dir_open,
876 	.release	= sysfs_dir_close,
877 	.llseek		= sysfs_dir_lseek,
878 	.read		= generic_read_dir,
879 	.readdir	= sysfs_readdir,
880 };
881