1919#include <linux/mutex.h>
2020#include <linux/idr.h>
2121#include <linux/log2.h>
22+ #include <linux/ctype.h>
2223
2324#include "blk.h"
2425
@@ -909,6 +910,74 @@ static int __init genhd_device_init(void)
909910
910911subsys_initcall (genhd_device_init );
911912
913+ static ssize_t alias_show (struct device * dev ,
914+ struct device_attribute * attr , char * buf )
915+ {
916+ struct gendisk * disk = dev_to_disk (dev );
917+ ssize_t ret = 0 ;
918+
919+ if (disk -> alias )
920+ ret = snprintf (buf , ALIAS_LEN , "%s\n" , disk -> alias );
921+ return ret ;
922+ }
923+
924+ static ssize_t alias_store (struct device * dev , struct device_attribute * attr ,
925+ const char * buf , size_t count )
926+ {
927+ struct gendisk * disk = dev_to_disk (dev );
928+ char * alias ;
929+ char * envp [] = { NULL , NULL };
930+ unsigned char c ;
931+ int i ;
932+ ssize_t ret = count ;
933+
934+ if (!count )
935+ return - EINVAL ;
936+
937+ if (count >= ALIAS_LEN ) {
938+ printk (KERN_ERR "alias: alias is too long\n" );
939+ return - EINVAL ;
940+ }
941+
942+ /* Validation check */
943+ for (i = 0 ; i < count ; i ++ ) {
944+ c = buf [i ];
945+ if (i == count - 1 && c == '\n' )
946+ break ;
947+ if (!isalnum (c ) && c != '_' && c != '-' ) {
948+ printk (KERN_ERR "alias: invalid alias\n" );
949+ return - EINVAL ;
950+ }
951+ }
952+
953+ if (disk -> alias ) {
954+ printk (KERN_INFO "alias: %s is already assigned (%s)\n" ,
955+ disk -> disk_name , disk -> alias );
956+ return - EINVAL ;
957+ }
958+
959+ alias = kasprintf (GFP_KERNEL , "%s" , buf );
960+ if (!alias )
961+ return - ENOMEM ;
962+
963+ if (alias [count - 1 ] == '\n' )
964+ alias [count - 1 ] = '\0' ;
965+
966+ envp [0 ] = kasprintf (GFP_KERNEL , "ALIAS=%s" , alias );
967+ if (!envp [0 ]) {
968+ kfree (alias );
969+ return - ENOMEM ;
970+ }
971+
972+ disk -> alias = alias ;
973+ printk (KERN_INFO "alias: assigned %s to %s\n" , alias , disk -> disk_name );
974+
975+ kobject_uevent_env (& dev -> kobj , KOBJ_ADD , envp );
976+
977+ kfree (envp [0 ]);
978+ return ret ;
979+ }
980+
912981static ssize_t disk_range_show (struct device * dev ,
913982 struct device_attribute * attr , char * buf )
914983{
@@ -968,6 +1037,7 @@ static ssize_t disk_discard_alignment_show(struct device *dev,
9681037 return sprintf (buf , "%d\n" , queue_discard_alignment (disk -> queue ));
9691038}
9701039
1040+ static DEVICE_ATTR (alias , S_IRUGO |S_IWUSR , alias_show , alias_store ) ;
9711041static DEVICE_ATTR (range , S_IRUGO , disk_range_show , NULL) ;
9721042static DEVICE_ATTR (ext_range , S_IRUGO , disk_ext_range_show , NULL) ;
9731043static DEVICE_ATTR (removable , S_IRUGO , disk_removable_show , NULL) ;
@@ -990,6 +1060,7 @@ static struct device_attribute dev_attr_fail_timeout =
9901060#endif
9911061
9921062static struct attribute * disk_attrs [] = {
1063+ & dev_attr_alias .attr ,
9931064 & dev_attr_range .attr ,
9941065 & dev_attr_ext_range .attr ,
9951066 & dev_attr_removable .attr ,
0 commit comments