diff options
Diffstat (limited to 'fs/sysfs/dir.c')
| -rw-r--r-- | fs/sysfs/dir.c | 114 |
1 files changed, 90 insertions, 24 deletions
diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c index 590717861c7a..7e54bac8c4b0 100644 --- a/fs/sysfs/dir.c +++ b/fs/sysfs/dir.c | |||
| @@ -380,7 +380,7 @@ int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd) | |||
| 380 | { | 380 | { |
| 381 | struct sysfs_inode_attrs *ps_iattr; | 381 | struct sysfs_inode_attrs *ps_iattr; |
| 382 | 382 | ||
| 383 | if (sysfs_find_dirent(acxt->parent_sd, sd->s_name)) | 383 | if (sysfs_find_dirent(acxt->parent_sd, sd->s_ns, sd->s_name)) |
| 384 | return -EEXIST; | 384 | return -EEXIST; |
| 385 | 385 | ||
| 386 | sd->s_parent = sysfs_get(acxt->parent_sd); | 386 | sd->s_parent = sysfs_get(acxt->parent_sd); |
| @@ -533,13 +533,17 @@ void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt) | |||
| 533 | * Pointer to sysfs_dirent if found, NULL if not. | 533 | * Pointer to sysfs_dirent if found, NULL if not. |
| 534 | */ | 534 | */ |
| 535 | struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd, | 535 | struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd, |
| 536 | const void *ns, | ||
| 536 | const unsigned char *name) | 537 | const unsigned char *name) |
| 537 | { | 538 | { |
| 538 | struct sysfs_dirent *sd; | 539 | struct sysfs_dirent *sd; |
| 539 | 540 | ||
| 540 | for (sd = parent_sd->s_dir.children; sd; sd = sd->s_sibling) | 541 | for (sd = parent_sd->s_dir.children; sd; sd = sd->s_sibling) { |
| 542 | if (ns && sd->s_ns && (sd->s_ns != ns)) | ||
| 543 | continue; | ||
| 541 | if (!strcmp(sd->s_name, name)) | 544 | if (!strcmp(sd->s_name, name)) |
| 542 | return sd; | 545 | return sd; |
| 546 | } | ||
| 543 | return NULL; | 547 | return NULL; |
| 544 | } | 548 | } |
| 545 | 549 | ||
| @@ -558,12 +562,13 @@ struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd, | |||
| 558 | * Pointer to sysfs_dirent if found, NULL if not. | 562 | * Pointer to sysfs_dirent if found, NULL if not. |
| 559 | */ | 563 | */ |
| 560 | struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd, | 564 | struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd, |
| 565 | const void *ns, | ||
| 561 | const unsigned char *name) | 566 | const unsigned char *name) |
| 562 | { | 567 | { |
| 563 | struct sysfs_dirent *sd; | 568 | struct sysfs_dirent *sd; |
| 564 | 569 | ||
| 565 | mutex_lock(&sysfs_mutex); | 570 | mutex_lock(&sysfs_mutex); |
| 566 | sd = sysfs_find_dirent(parent_sd, name); | 571 | sd = sysfs_find_dirent(parent_sd, ns, name); |
| 567 | sysfs_get(sd); | 572 | sysfs_get(sd); |
| 568 | mutex_unlock(&sysfs_mutex); | 573 | mutex_unlock(&sysfs_mutex); |
| 569 | 574 | ||
| @@ -572,7 +577,8 @@ struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd, | |||
| 572 | EXPORT_SYMBOL_GPL(sysfs_get_dirent); | 577 | EXPORT_SYMBOL_GPL(sysfs_get_dirent); |
| 573 | 578 | ||
| 574 | static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, | 579 | static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, |
| 575 | const char *name, struct sysfs_dirent **p_sd) | 580 | enum kobj_ns_type type, const void *ns, const char *name, |
| 581 | struct sysfs_dirent **p_sd) | ||
| 576 | { | 582 | { |
| 577 | umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; | 583 | umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; |
| 578 | struct sysfs_addrm_cxt acxt; | 584 | struct sysfs_addrm_cxt acxt; |
| @@ -583,6 +589,9 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, | |||
| 583 | sd = sysfs_new_dirent(name, mode, SYSFS_DIR); | 589 | sd = sysfs_new_dirent(name, mode, SYSFS_DIR); |
| 584 | if (!sd) | 590 | if (!sd) |
| 585 | return -ENOMEM; | 591 | return -ENOMEM; |
| 592 | |||
| 593 | sd->s_flags |= (type << SYSFS_NS_TYPE_SHIFT); | ||
| 594 | sd->s_ns = ns; | ||
| 586 | sd->s_dir.kobj = kobj; | 595 | sd->s_dir.kobj = kobj; |
| 587 | 596 | ||
| 588 | /* link in */ | 597 | /* link in */ |
| @@ -601,7 +610,33 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, | |||
| 601 | int sysfs_create_subdir(struct kobject *kobj, const char *name, | 610 | int sysfs_create_subdir(struct kobject *kobj, const char *name, |
| 602 | struct sysfs_dirent **p_sd) | 611 | struct sysfs_dirent **p_sd) |
| 603 | { | 612 | { |
| 604 | return create_dir(kobj, kobj->sd, name, p_sd); | 613 | return create_dir(kobj, kobj->sd, |
| 614 | KOBJ_NS_TYPE_NONE, NULL, name, p_sd); | ||
| 615 | } | ||
| 616 | |||
| 617 | /** | ||
| 618 | * sysfs_read_ns_type: return associated ns_type | ||
| 619 | * @kobj: the kobject being queried | ||
| 620 | * | ||
| 621 | * Each kobject can be tagged with exactly one namespace type | ||
| 622 | * (i.e. network or user). Return the ns_type associated with | ||
| 623 | * this object if any | ||
| 624 | */ | ||
| 625 | static enum kobj_ns_type sysfs_read_ns_type(struct kobject *kobj) | ||
| 626 | { | ||
| 627 | const struct kobj_ns_type_operations *ops; | ||
| 628 | enum kobj_ns_type type; | ||
| 629 | |||
| 630 | ops = kobj_child_ns_ops(kobj); | ||
| 631 | if (!ops) | ||
| 632 | return KOBJ_NS_TYPE_NONE; | ||
| 633 | |||
| 634 | type = ops->type; | ||
| 635 | BUG_ON(type <= KOBJ_NS_TYPE_NONE); | ||
| 636 | BUG_ON(type >= KOBJ_NS_TYPES); | ||
| 637 | BUG_ON(!kobj_ns_type_registered(type)); | ||
| 638 | |||
| 639 | return type; | ||
| 605 | } | 640 | } |
| 606 | 641 | ||
| 607 | /** | 642 | /** |
| @@ -610,7 +645,9 @@ int sysfs_create_subdir(struct kobject *kobj, const char *name, | |||
| 610 | */ | 645 | */ |
| 611 | int sysfs_create_dir(struct kobject * kobj) | 646 | int sysfs_create_dir(struct kobject * kobj) |
| 612 | { | 647 | { |
| 648 | enum kobj_ns_type type; | ||
| 613 | struct sysfs_dirent *parent_sd, *sd; | 649 | struct sysfs_dirent *parent_sd, *sd; |
| 650 | const void *ns = NULL; | ||
| 614 | int error = 0; | 651 | int error = 0; |
| 615 | 652 | ||
| 616 | BUG_ON(!kobj); | 653 | BUG_ON(!kobj); |
| @@ -620,7 +657,11 @@ int sysfs_create_dir(struct kobject * kobj) | |||
| 620 | else | 657 | else |
| 621 | parent_sd = &sysfs_root; | 658 | parent_sd = &sysfs_root; |
| 622 | 659 | ||
| 623 | error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd); | 660 | if (sysfs_ns_type(parent_sd)) |
| 661 | ns = kobj->ktype->namespace(kobj); | ||
| 662 | type = sysfs_read_ns_type(kobj); | ||
| 663 | |||
| 664 | error = create_dir(kobj, parent_sd, type, ns, kobject_name(kobj), &sd); | ||
| 624 | if (!error) | 665 | if (!error) |
| 625 | kobj->sd = sd; | 666 | kobj->sd = sd; |
| 626 | return error; | 667 | return error; |
| @@ -630,13 +671,19 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, | |||
| 630 | struct nameidata *nd) | 671 | struct nameidata *nd) |
| 631 | { | 672 | { |
| 632 | struct dentry *ret = NULL; | 673 | struct dentry *ret = NULL; |
| 633 | struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata; | 674 | struct dentry *parent = dentry->d_parent; |
| 675 | struct sysfs_dirent *parent_sd = parent->d_fsdata; | ||
| 634 | struct sysfs_dirent *sd; | 676 | struct sysfs_dirent *sd; |
| 635 | struct inode *inode; | 677 | struct inode *inode; |
| 678 | enum kobj_ns_type type; | ||
| 679 | const void *ns; | ||
| 636 | 680 | ||
| 637 | mutex_lock(&sysfs_mutex); | 681 | mutex_lock(&sysfs_mutex); |
| 638 | 682 | ||
| 639 | sd = sysfs_find_dirent(parent_sd, dentry->d_name.name); | 683 | type = sysfs_ns_type(parent_sd); |
| 684 | ns = sysfs_info(dir->i_sb)->ns[type]; | ||
| 685 | |||
| 686 | sd = sysfs_find_dirent(parent_sd, ns, dentry->d_name.name); | ||
| 640 | 687 | ||
| 641 | /* no such entry */ | 688 | /* no such entry */ |
| 642 | if (!sd) { | 689 | if (!sd) { |
| @@ -735,7 +782,8 @@ void sysfs_remove_dir(struct kobject * kobj) | |||
| 735 | } | 782 | } |
| 736 | 783 | ||
| 737 | int sysfs_rename(struct sysfs_dirent *sd, | 784 | int sysfs_rename(struct sysfs_dirent *sd, |
| 738 | struct sysfs_dirent *new_parent_sd, const char *new_name) | 785 | struct sysfs_dirent *new_parent_sd, const void *new_ns, |
| 786 | const char *new_name) | ||
| 739 | { | 787 | { |
| 740 | const char *dup_name = NULL; | 788 | const char *dup_name = NULL; |
| 741 | int error; | 789 | int error; |
| @@ -743,12 +791,12 @@ int sysfs_rename(struct sysfs_dirent *sd, | |||
| 743 | mutex_lock(&sysfs_mutex); | 791 | mutex_lock(&sysfs_mutex); |
| 744 | 792 | ||
| 745 | error = 0; | 793 | error = 0; |
| 746 | if ((sd->s_parent == new_parent_sd) && | 794 | if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) && |
| 747 | (strcmp(sd->s_name, new_name) == 0)) | 795 | (strcmp(sd->s_name, new_name) == 0)) |
| 748 | goto out; /* nothing to rename */ | 796 | goto out; /* nothing to rename */ |
| 749 | 797 | ||
| 750 | error = -EEXIST; | 798 | error = -EEXIST; |
| 751 | if (sysfs_find_dirent(new_parent_sd, new_name)) | 799 | if (sysfs_find_dirent(new_parent_sd, new_ns, new_name)) |
| 752 | goto out; | 800 | goto out; |
| 753 | 801 | ||
| 754 | /* rename sysfs_dirent */ | 802 | /* rename sysfs_dirent */ |
| @@ -770,6 +818,7 @@ int sysfs_rename(struct sysfs_dirent *sd, | |||
| 770 | sd->s_parent = new_parent_sd; | 818 | sd->s_parent = new_parent_sd; |
| 771 | sysfs_link_sibling(sd); | 819 | sysfs_link_sibling(sd); |
| 772 | } | 820 | } |
| 821 | sd->s_ns = new_ns; | ||
| 773 | 822 | ||
| 774 | error = 0; | 823 | error = 0; |
| 775 | out: | 824 | out: |
| @@ -780,19 +829,28 @@ int sysfs_rename(struct sysfs_dirent *sd, | |||
| 780 | 829 | ||
| 781 | int sysfs_rename_dir(struct kobject *kobj, const char *new_name) | 830 | int sysfs_rename_dir(struct kobject *kobj, const char *new_name) |
| 782 | { | 831 | { |
| 783 | return sysfs_rename(kobj->sd, kobj->sd->s_parent, new_name); | 832 | struct sysfs_dirent *parent_sd = kobj->sd->s_parent; |
| 833 | const void *new_ns = NULL; | ||
| 834 | |||
| 835 | if (sysfs_ns_type(parent_sd)) | ||
| 836 | new_ns = kobj->ktype->namespace(kobj); | ||
| 837 | |||
| 838 | return sysfs_rename(kobj->sd, parent_sd, new_ns, new_name); | ||
| 784 | } | 839 | } |
| 785 | 840 | ||
| 786 | int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj) | 841 | int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj) |
| 787 | { | 842 | { |
| 788 | struct sysfs_dirent *sd = kobj->sd; | 843 | struct sysfs_dirent *sd = kobj->sd; |
| 789 | struct sysfs_dirent *new_parent_sd; | 844 | struct sysfs_dirent *new_parent_sd; |
| 845 | const void *new_ns = NULL; | ||
| 790 | 846 | ||
| 791 | BUG_ON(!sd->s_parent); | 847 | BUG_ON(!sd->s_parent); |
| 848 | if (sysfs_ns_type(sd->s_parent)) | ||
| 849 | new_ns = kobj->ktype->namespace(kobj); | ||
| 792 | new_parent_sd = new_parent_kobj && new_parent_kobj->sd ? | 850 | new_parent_sd = new_parent_kobj && new_parent_kobj->sd ? |
| 793 | new_parent_kobj->sd : &sysfs_root; | 851 | new_parent_kobj->sd : &sysfs_root; |
| 794 | 852 | ||
| 795 | return sysfs_rename(sd, new_parent_sd, sd->s_name); | 853 | return sysfs_rename(sd, new_parent_sd, new_ns, sd->s_name); |
| 796 | } | 854 | } |
| 797 | 855 | ||
| 798 | /* Relationship between s_mode and the DT_xxx types */ | 856 | /* Relationship between s_mode and the DT_xxx types */ |
| @@ -807,32 +865,35 @@ static int sysfs_dir_release(struct inode *inode, struct file *filp) | |||
| 807 | return 0; | 865 | return 0; |
| 808 | } | 866 | } |
| 809 | 867 | ||
| 810 | static struct sysfs_dirent *sysfs_dir_pos(struct sysfs_dirent *parent_sd, | 868 | static struct sysfs_dirent *sysfs_dir_pos(const void *ns, |
| 811 | ino_t ino, struct sysfs_dirent *pos) | 869 | struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos) |
| 812 | { | 870 | { |
| 813 | if (pos) { | 871 | if (pos) { |
| 814 | int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) && | 872 | int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) && |
| 815 | pos->s_parent == parent_sd && | 873 | pos->s_parent == parent_sd && |
| 816 | ino == pos->s_ino; | 874 | ino == pos->s_ino; |
| 817 | sysfs_put(pos); | 875 | sysfs_put(pos); |
| 818 | if (valid) | 876 | if (!valid) |
| 819 | return pos; | 877 | pos = NULL; |
| 820 | } | 878 | } |
| 821 | pos = NULL; | 879 | if (!pos && (ino > 1) && (ino < INT_MAX)) { |
| 822 | if ((ino > 1) && (ino < INT_MAX)) { | ||
| 823 | pos = parent_sd->s_dir.children; | 880 | pos = parent_sd->s_dir.children; |
| 824 | while (pos && (ino > pos->s_ino)) | 881 | while (pos && (ino > pos->s_ino)) |
| 825 | pos = pos->s_sibling; | 882 | pos = pos->s_sibling; |
| 826 | } | 883 | } |
| 884 | while (pos && pos->s_ns && pos->s_ns != ns) | ||
| 885 | pos = pos->s_sibling; | ||
| 827 | return pos; | 886 | return pos; |
| 828 | } | 887 | } |
| 829 | 888 | ||
| 830 | static struct sysfs_dirent *sysfs_dir_next_pos(struct sysfs_dirent *parent_sd, | 889 | static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns, |
| 831 | ino_t ino, struct sysfs_dirent *pos) | 890 | struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos) |
| 832 | { | 891 | { |
| 833 | pos = sysfs_dir_pos(parent_sd, ino, pos); | 892 | pos = sysfs_dir_pos(ns, parent_sd, ino, pos); |
| 834 | if (pos) | 893 | if (pos) |
| 835 | pos = pos->s_sibling; | 894 | pos = pos->s_sibling; |
| 895 | while (pos && pos->s_ns && pos->s_ns != ns) | ||
| 896 | pos = pos->s_sibling; | ||
| 836 | return pos; | 897 | return pos; |
| 837 | } | 898 | } |
| 838 | 899 | ||
| @@ -841,8 +902,13 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) | |||
| 841 | struct dentry *dentry = filp->f_path.dentry; | 902 | struct dentry *dentry = filp->f_path.dentry; |
| 842 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; | 903 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; |
| 843 | struct sysfs_dirent *pos = filp->private_data; | 904 | struct sysfs_dirent *pos = filp->private_data; |
| 905 | enum kobj_ns_type type; | ||
| 906 | const void *ns; | ||
| 844 | ino_t ino; | 907 | ino_t ino; |
| 845 | 908 | ||
| 909 | type = sysfs_ns_type(parent_sd); | ||
| 910 | ns = sysfs_info(dentry->d_sb)->ns[type]; | ||
| 911 | |||
| 846 | if (filp->f_pos == 0) { | 912 | if (filp->f_pos == 0) { |
| 847 | ino = parent_sd->s_ino; | 913 | ino = parent_sd->s_ino; |
| 848 | if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0) | 914 | if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0) |
| @@ -857,9 +923,9 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) | |||
| 857 | filp->f_pos++; | 923 | filp->f_pos++; |
| 858 | } | 924 | } |
| 859 | mutex_lock(&sysfs_mutex); | 925 | mutex_lock(&sysfs_mutex); |
| 860 | for (pos = sysfs_dir_pos(parent_sd, filp->f_pos, pos); | 926 | for (pos = sysfs_dir_pos(ns, parent_sd, filp->f_pos, pos); |
| 861 | pos; | 927 | pos; |
| 862 | pos = sysfs_dir_next_pos(parent_sd, filp->f_pos, pos)) { | 928 | pos = sysfs_dir_next_pos(ns, parent_sd, filp->f_pos, pos)) { |
| 863 | const char * name; | 929 | const char * name; |
| 864 | unsigned int type; | 930 | unsigned int type; |
| 865 | int len, ret; | 931 | int len, ret; |
