3434#include <linux/ceph/cls_lock_client.h>
3535#include <linux/ceph/striper.h>
3636#include <linux/ceph/decode.h>
37- #include <linux/parser .h>
37+ #include <linux/fs_parser .h>
3838#include <linux/bsearch.h>
3939
4040#include <linux/kernel.h>
@@ -838,34 +838,34 @@ enum {
838838 Opt_queue_depth ,
839839 Opt_alloc_size ,
840840 Opt_lock_timeout ,
841- Opt_last_int ,
842841 /* int args above */
843842 Opt_pool_ns ,
844- Opt_last_string ,
845843 /* string args above */
846844 Opt_read_only ,
847845 Opt_read_write ,
848846 Opt_lock_on_read ,
849847 Opt_exclusive ,
850848 Opt_notrim ,
851- Opt_err
852849};
853850
854- static match_table_t rbd_opts_tokens = {
855- {Opt_queue_depth , "queue_depth=%d" },
856- {Opt_alloc_size , "alloc_size=%d" },
857- {Opt_lock_timeout , "lock_timeout=%d" },
858- /* int args above */
859- {Opt_pool_ns , "_pool_ns=%s" },
860- /* string args above */
861- {Opt_read_only , "read_only" },
862- {Opt_read_only , "ro" }, /* Alternate spelling */
863- {Opt_read_write , "read_write" },
864- {Opt_read_write , "rw" }, /* Alternate spelling */
865- {Opt_lock_on_read , "lock_on_read" },
866- {Opt_exclusive , "exclusive" },
867- {Opt_notrim , "notrim" },
868- {Opt_err , NULL }
851+ static const struct fs_parameter_spec rbd_param_specs [] = {
852+ fsparam_u32 ("alloc_size" , Opt_alloc_size ),
853+ fsparam_flag ("exclusive" , Opt_exclusive ),
854+ fsparam_flag ("lock_on_read" , Opt_lock_on_read ),
855+ fsparam_u32 ("lock_timeout" , Opt_lock_timeout ),
856+ fsparam_flag ("notrim" , Opt_notrim ),
857+ fsparam_string ("_pool_ns" , Opt_pool_ns ),
858+ fsparam_u32 ("queue_depth" , Opt_queue_depth ),
859+ fsparam_flag ("read_only" , Opt_read_only ),
860+ fsparam_flag ("read_write" , Opt_read_write ),
861+ fsparam_flag ("ro" , Opt_read_only ),
862+ fsparam_flag ("rw" , Opt_read_write ),
863+ {}
864+ };
865+
866+ static const struct fs_parameter_description rbd_parameters = {
867+ .name = "rbd" ,
868+ .specs = rbd_param_specs ,
869869};
870870
871871struct rbd_options {
@@ -886,87 +886,12 @@ struct rbd_options {
886886#define RBD_EXCLUSIVE_DEFAULT false
887887#define RBD_TRIM_DEFAULT true
888888
889- struct parse_rbd_opts_ctx {
889+ struct rbd_parse_opts_ctx {
890890 struct rbd_spec * spec ;
891+ struct ceph_options * copts ;
891892 struct rbd_options * opts ;
892893};
893894
894- static int parse_rbd_opts_token (char * c , void * private )
895- {
896- struct parse_rbd_opts_ctx * pctx = private ;
897- substring_t argstr [MAX_OPT_ARGS ];
898- int token , intval , ret ;
899-
900- token = match_token (c , rbd_opts_tokens , argstr );
901- if (token < Opt_last_int ) {
902- ret = match_int (& argstr [0 ], & intval );
903- if (ret < 0 ) {
904- pr_err ("bad option arg (not int) at '%s'\n" , c );
905- return ret ;
906- }
907- dout ("got int token %d val %d\n" , token , intval );
908- } else if (token > Opt_last_int && token < Opt_last_string ) {
909- dout ("got string token %d val %s\n" , token , argstr [0 ].from );
910- } else {
911- dout ("got token %d\n" , token );
912- }
913-
914- switch (token ) {
915- case Opt_queue_depth :
916- if (intval < 1 ) {
917- pr_err ("queue_depth out of range\n" );
918- return - EINVAL ;
919- }
920- pctx -> opts -> queue_depth = intval ;
921- break ;
922- case Opt_alloc_size :
923- if (intval < SECTOR_SIZE ) {
924- pr_err ("alloc_size out of range\n" );
925- return - EINVAL ;
926- }
927- if (!is_power_of_2 (intval )) {
928- pr_err ("alloc_size must be a power of 2\n" );
929- return - EINVAL ;
930- }
931- pctx -> opts -> alloc_size = intval ;
932- break ;
933- case Opt_lock_timeout :
934- /* 0 is "wait forever" (i.e. infinite timeout) */
935- if (intval < 0 || intval > INT_MAX / 1000 ) {
936- pr_err ("lock_timeout out of range\n" );
937- return - EINVAL ;
938- }
939- pctx -> opts -> lock_timeout = msecs_to_jiffies (intval * 1000 );
940- break ;
941- case Opt_pool_ns :
942- kfree (pctx -> spec -> pool_ns );
943- pctx -> spec -> pool_ns = match_strdup (argstr );
944- if (!pctx -> spec -> pool_ns )
945- return - ENOMEM ;
946- break ;
947- case Opt_read_only :
948- pctx -> opts -> read_only = true;
949- break ;
950- case Opt_read_write :
951- pctx -> opts -> read_only = false;
952- break ;
953- case Opt_lock_on_read :
954- pctx -> opts -> lock_on_read = true;
955- break ;
956- case Opt_exclusive :
957- pctx -> opts -> exclusive = true;
958- break ;
959- case Opt_notrim :
960- pctx -> opts -> trim = false;
961- break ;
962- default :
963- /* libceph prints "bad option" msg */
964- return - EINVAL ;
965- }
966-
967- return 0 ;
968- }
969-
970895static char * obj_op_name (enum obj_operation_type op_type )
971896{
972897 switch (op_type ) {
@@ -6423,6 +6348,122 @@ static inline char *dup_token(const char **buf, size_t *lenp)
64236348 return dup ;
64246349}
64256350
6351+ static int rbd_parse_param (struct fs_parameter * param ,
6352+ struct rbd_parse_opts_ctx * pctx )
6353+ {
6354+ struct rbd_options * opt = pctx -> opts ;
6355+ struct fs_parse_result result ;
6356+ int token , ret ;
6357+
6358+ ret = ceph_parse_param (param , pctx -> copts , NULL );
6359+ if (ret != - ENOPARAM )
6360+ return ret ;
6361+
6362+ token = fs_parse (NULL , & rbd_parameters , param , & result );
6363+ dout ("%s fs_parse '%s' token %d\n" , __func__ , param -> key , token );
6364+ if (token < 0 ) {
6365+ if (token == - ENOPARAM ) {
6366+ return invalf (NULL , "rbd: Unknown parameter '%s'" ,
6367+ param -> key );
6368+ }
6369+ return token ;
6370+ }
6371+
6372+ switch (token ) {
6373+ case Opt_queue_depth :
6374+ if (result .uint_32 < 1 )
6375+ goto out_of_range ;
6376+ opt -> queue_depth = result .uint_32 ;
6377+ break ;
6378+ case Opt_alloc_size :
6379+ if (result .uint_32 < SECTOR_SIZE )
6380+ goto out_of_range ;
6381+ if (!is_power_of_2 (result .uint_32 )) {
6382+ return invalf (NULL , "rbd: alloc_size must be a power of 2" );
6383+ }
6384+ opt -> alloc_size = result .uint_32 ;
6385+ break ;
6386+ case Opt_lock_timeout :
6387+ /* 0 is "wait forever" (i.e. infinite timeout) */
6388+ if (result .uint_32 > INT_MAX / 1000 )
6389+ goto out_of_range ;
6390+ opt -> lock_timeout = msecs_to_jiffies (result .uint_32 * 1000 );
6391+ break ;
6392+ case Opt_pool_ns :
6393+ kfree (pctx -> spec -> pool_ns );
6394+ pctx -> spec -> pool_ns = param -> string ;
6395+ param -> string = NULL ;
6396+ break ;
6397+ case Opt_read_only :
6398+ opt -> read_only = true;
6399+ break ;
6400+ case Opt_read_write :
6401+ opt -> read_only = false;
6402+ break ;
6403+ case Opt_lock_on_read :
6404+ opt -> lock_on_read = true;
6405+ break ;
6406+ case Opt_exclusive :
6407+ opt -> exclusive = true;
6408+ break ;
6409+ case Opt_notrim :
6410+ opt -> trim = false;
6411+ break ;
6412+ default :
6413+ BUG ();
6414+ }
6415+
6416+ return 0 ;
6417+
6418+ out_of_range :
6419+ return invalf (NULL , "rbd: %s out of range" , param -> key );
6420+ }
6421+
6422+ /*
6423+ * This duplicates most of generic_parse_monolithic(), untying it from
6424+ * fs_context and skipping standard superblock and security options.
6425+ */
6426+ static int rbd_parse_options (char * options , struct rbd_parse_opts_ctx * pctx )
6427+ {
6428+ char * key ;
6429+ int ret = 0 ;
6430+
6431+ dout ("%s '%s'\n" , __func__ , options );
6432+ while ((key = strsep (& options , "," )) != NULL ) {
6433+ if (* key ) {
6434+ struct fs_parameter param = {
6435+ .key = key ,
6436+ .type = fs_value_is_string ,
6437+ };
6438+ char * value = strchr (key , '=' );
6439+ size_t v_len = 0 ;
6440+
6441+ if (value ) {
6442+ if (value == key )
6443+ continue ;
6444+ * value ++ = 0 ;
6445+ v_len = strlen (value );
6446+ }
6447+
6448+
6449+ if (v_len > 0 ) {
6450+ param .string = kmemdup_nul (value , v_len ,
6451+ GFP_KERNEL );
6452+ if (!param .string )
6453+ return - ENOMEM ;
6454+ }
6455+ param .size = v_len ;
6456+
6457+ ret = rbd_parse_param (& param , pctx );
6458+ kfree (param .string );
6459+ if (ret )
6460+ break ;
6461+ }
6462+ }
6463+
6464+ return ret ;
6465+ }
6466+
64266467/*
64276468 * Parse the options provided for an "rbd add" (i.e., rbd image
64286469 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
@@ -6474,8 +6515,7 @@ static int rbd_add_parse_args(const char *buf,
64746515 const char * mon_addrs ;
64756516 char * snap_name ;
64766517 size_t mon_addrs_size ;
6477- struct parse_rbd_opts_ctx pctx = { 0 };
6478- struct ceph_options * copts ;
6518+ struct rbd_parse_opts_ctx pctx = { 0 };
64796519 int ret ;
64806520
64816521 /* The first four tokens are required */
@@ -6486,7 +6526,7 @@ static int rbd_add_parse_args(const char *buf,
64866526 return - EINVAL ;
64876527 }
64886528 mon_addrs = buf ;
6489- mon_addrs_size = len + 1 ;
6529+ mon_addrs_size = len ;
64906530 buf += len ;
64916531
64926532 ret = - EINVAL ;
@@ -6536,6 +6576,10 @@ static int rbd_add_parse_args(const char *buf,
65366576 * (snap_name + len ) = '\0' ;
65376577 pctx .spec -> snap_name = snap_name ;
65386578
6579+ pctx .copts = ceph_alloc_options ();
6580+ if (!pctx .copts )
6581+ goto out_mem ;
6582+
65396583 /* Initialize all rbd options to the defaults */
65406584
65416585 pctx .opts = kzalloc (sizeof (* pctx .opts ), GFP_KERNEL );
@@ -6550,27 +6594,27 @@ static int rbd_add_parse_args(const char *buf,
65506594 pctx .opts -> exclusive = RBD_EXCLUSIVE_DEFAULT ;
65516595 pctx .opts -> trim = RBD_TRIM_DEFAULT ;
65526596
6553- copts = ceph_parse_options (options , mon_addrs ,
6554- mon_addrs + mon_addrs_size - 1 ,
6555- parse_rbd_opts_token , & pctx );
6556- if (IS_ERR (copts )) {
6557- ret = PTR_ERR (copts );
6597+ ret = ceph_parse_mon_ips (mon_addrs , mon_addrs_size , pctx .copts , NULL );
6598+ if (ret )
65586599 goto out_err ;
6559- }
6560- kfree (options );
65616600
6562- * ceph_opts = copts ;
6601+ ret = rbd_parse_options (options , & pctx );
6602+ if (ret )
6603+ goto out_err ;
6604+
6605+ * ceph_opts = pctx .copts ;
65636606 * opts = pctx .opts ;
65646607 * rbd_spec = pctx .spec ;
6565-
6608+ kfree ( options );
65666609 return 0 ;
6610+
65676611out_mem :
65686612 ret = - ENOMEM ;
65696613out_err :
65706614 kfree (pctx .opts );
6615+ ceph_destroy_options (pctx .copts );
65716616 rbd_spec_put (pctx .spec );
65726617 kfree (options );
6573-
65746618 return ret ;
65756619}
65766620
0 commit comments