params.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /* Helpers for initial module or kernel cmdline parsing
  2. Copyright (C) 2001 Rusty Russell.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include <linux/moduleparam.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/errno.h>
  19. #include <linux/module.h>
  20. #include <linux/device.h>
  21. #include <linux/err.h>
  22. #include <linux/slab.h>
  23. #include <linux/ctype.h>
  24. #if 0
  25. #define DEBUGP printk
  26. #else
  27. #define DEBUGP(fmt, a...)
  28. #endif
  29. static inline char dash2underscore(char c)
  30. {
  31. if (c == '-')
  32. return '_';
  33. return c;
  34. }
  35. static inline int parameq(const char *input, const char *paramname)
  36. {
  37. unsigned int i;
  38. for (i = 0; dash2underscore(input[i]) == paramname[i]; i++)
  39. if (input[i] == '\0')
  40. return 1;
  41. return 0;
  42. }
  43. static int parse_one(char *param,
  44. char *val,
  45. struct kernel_param *params,
  46. unsigned num_params,
  47. int (*handle_unknown)(char *param, char *val))
  48. {
  49. unsigned int i;
  50. /* Find parameter */
  51. for (i = 0; i < num_params; i++) {
  52. if (parameq(param, params[i].name)) {
  53. /* Noone handled NULL, so do it here. */
  54. if (!val && params[i].ops->set != param_set_bool)
  55. return -EINVAL;
  56. DEBUGP("They are equal! Calling %p\n",
  57. params[i].ops->set);
  58. return params[i].ops->set(val, &params[i]);
  59. }
  60. }
  61. if (handle_unknown) {
  62. DEBUGP("Unknown argument: calling %p\n", handle_unknown);
  63. return handle_unknown(param, val);
  64. }
  65. DEBUGP("Unknown argument `%s'\n", param);
  66. return -ENOENT;
  67. }
  68. /* You can use " around spaces, but can't escape ". */
  69. /* Hyphens and underscores equivalent in parameter names. */
  70. static char *next_arg(char *args, char **param, char **val)
  71. {
  72. unsigned int i, equals = 0;
  73. int in_quote = 0, quoted = 0;
  74. char *next;
  75. if (*args == '"') {
  76. args++;
  77. in_quote = 1;
  78. quoted = 1;
  79. }
  80. for (i = 0; args[i]; i++) {
  81. if (isspace(args[i]) && !in_quote)
  82. break;
  83. if (equals == 0) {
  84. if (args[i] == '=')
  85. equals = i;
  86. }
  87. if (args[i] == '"')
  88. in_quote = !in_quote;
  89. }
  90. *param = args;
  91. if (!equals)
  92. *val = NULL;
  93. else {
  94. args[equals] = '\0';
  95. *val = args + equals + 1;
  96. /* Don't include quotes in value. */
  97. if (**val == '"') {
  98. (*val)++;
  99. if (args[i-1] == '"')
  100. args[i-1] = '\0';
  101. }
  102. if (quoted && args[i-1] == '"')
  103. args[i-1] = '\0';
  104. }
  105. if (args[i]) {
  106. args[i] = '\0';
  107. next = args + i + 1;
  108. } else
  109. next = args + i;
  110. /* Chew up trailing spaces. */
  111. return skip_spaces(next);
  112. }
  113. /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
  114. int parse_args(const char *name,
  115. char *args,
  116. struct kernel_param *params,
  117. unsigned num,
  118. int (*unknown)(char *param, char *val))
  119. {
  120. char *param, *val;
  121. DEBUGP("Parsing ARGS: %s\n", args);
  122. /* Chew leading spaces */
  123. args = skip_spaces(args);
  124. while (*args) {
  125. int ret;
  126. int irq_was_disabled;
  127. args = next_arg(args, &param, &val);
  128. irq_was_disabled = irqs_disabled();
  129. ret = parse_one(param, val, params, num, unknown);
  130. if (irq_was_disabled && !irqs_disabled()) {
  131. printk(KERN_WARNING "parse_args(): option '%s' enabled "
  132. "irq's!\n", param);
  133. }
  134. switch (ret) {
  135. case -ENOENT:
  136. printk(KERN_ERR "%s: Unknown parameter `%s'\n",
  137. name, param);
  138. return ret;
  139. case -ENOSPC:
  140. printk(KERN_ERR
  141. "%s: `%s' too large for parameter `%s'\n",
  142. name, val ?: "", param);
  143. return ret;
  144. case 0:
  145. break;
  146. default:
  147. printk(KERN_ERR
  148. "%s: `%s' invalid for parameter `%s'\n",
  149. name, val ?: "", param);
  150. return ret;
  151. }
  152. }
  153. /* All parsed OK. */
  154. return 0;
  155. }
  156. /* Lazy bastard, eh? */
  157. #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
  158. int param_set_##name(const char *val, const struct kernel_param *kp) \
  159. { \
  160. tmptype l; \
  161. int ret; \
  162. \
  163. ret = strtolfn(val, 0, &l); \
  164. if (ret == -EINVAL || ((type)l != l)) \
  165. return -EINVAL; \
  166. *((type *)kp->arg) = l; \
  167. return 0; \
  168. } \
  169. int param_get_##name(char *buffer, const struct kernel_param *kp) \
  170. { \
  171. return sprintf(buffer, format, *((type *)kp->arg)); \
  172. } \
  173. struct kernel_param_ops param_ops_##name = { \
  174. .set = param_set_##name, \
  175. .get = param_get_##name, \
  176. }; \
  177. EXPORT_SYMBOL(param_set_##name); \
  178. EXPORT_SYMBOL(param_get_##name); \
  179. EXPORT_SYMBOL(param_ops_##name)
  180. STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
  181. STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
  182. STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul);
  183. STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol);
  184. STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, strict_strtoul);
  185. STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol);
  186. STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, strict_strtoul);
  187. int param_set_charp(const char *val, const struct kernel_param *kp)
  188. {
  189. if (strlen(val) > 1024) {
  190. printk(KERN_ERR "%s: string parameter too long\n",
  191. kp->name);
  192. return -ENOSPC;
  193. }
  194. /* This is a hack. We can't need to strdup in early boot, and we
  195. * don't need to; this mangled commandline is preserved. */
  196. if (slab_is_available()) {
  197. *(char **)kp->arg = kstrdup(val, GFP_KERNEL);
  198. if (!*(char **)kp->arg)
  199. return -ENOMEM;
  200. } else
  201. *(const char **)kp->arg = val;
  202. return 0;
  203. }
  204. EXPORT_SYMBOL(param_set_charp);
  205. int param_get_charp(char *buffer, const struct kernel_param *kp)
  206. {
  207. return sprintf(buffer, "%s", *((char **)kp->arg));
  208. }
  209. EXPORT_SYMBOL(param_get_charp);
  210. struct kernel_param_ops param_ops_charp = {
  211. .set = param_set_charp,
  212. .get = param_get_charp,
  213. };
  214. EXPORT_SYMBOL(param_ops_charp);
  215. /* Actually could be a bool or an int, for historical reasons. */
  216. int param_set_bool(const char *val, const struct kernel_param *kp)
  217. {
  218. bool v;
  219. /* No equals means "set"... */
  220. if (!val) val = "1";
  221. /* One of =[yYnN01] */
  222. switch (val[0]) {
  223. case 'y': case 'Y': case '1':
  224. v = true;
  225. break;
  226. case 'n': case 'N': case '0':
  227. v = false;
  228. break;
  229. default:
  230. return -EINVAL;
  231. }
  232. if (kp->flags & KPARAM_ISBOOL)
  233. *(bool *)kp->arg = v;
  234. else
  235. *(int *)kp->arg = v;
  236. return 0;
  237. }
  238. EXPORT_SYMBOL(param_set_bool);
  239. int param_get_bool(char *buffer, const struct kernel_param *kp)
  240. {
  241. bool val;
  242. if (kp->flags & KPARAM_ISBOOL)
  243. val = *(bool *)kp->arg;
  244. else
  245. val = *(int *)kp->arg;
  246. /* Y and N chosen as being relatively non-coder friendly */
  247. return sprintf(buffer, "%c", val ? 'Y' : 'N');
  248. }
  249. EXPORT_SYMBOL(param_get_bool);
  250. struct kernel_param_ops param_ops_bool = {
  251. .set = param_set_bool,
  252. .get = param_get_bool,
  253. };
  254. EXPORT_SYMBOL(param_ops_bool);
  255. /* This one must be bool. */
  256. int param_set_invbool(const char *val, const struct kernel_param *kp)
  257. {
  258. int ret;
  259. bool boolval;
  260. struct kernel_param dummy;
  261. dummy.arg = &boolval;
  262. dummy.flags = KPARAM_ISBOOL;
  263. ret = param_set_bool(val, &dummy);
  264. if (ret == 0)
  265. *(bool *)kp->arg = !boolval;
  266. return ret;
  267. }
  268. EXPORT_SYMBOL(param_set_invbool);
  269. int param_get_invbool(char *buffer, const struct kernel_param *kp)
  270. {
  271. return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y');
  272. }
  273. EXPORT_SYMBOL(param_get_invbool);
  274. struct kernel_param_ops param_ops_invbool = {
  275. .set = param_set_invbool,
  276. .get = param_get_invbool,
  277. };
  278. EXPORT_SYMBOL(param_ops_invbool);
  279. /* We break the rule and mangle the string. */
  280. static int param_array(const char *name,
  281. const char *val,
  282. unsigned int min, unsigned int max,
  283. void *elem, int elemsize,
  284. int (*set)(const char *, const struct kernel_param *kp),
  285. u16 flags,
  286. unsigned int *num)
  287. {
  288. int ret;
  289. struct kernel_param kp;
  290. char save;
  291. /* Get the name right for errors. */
  292. kp.name = name;
  293. kp.arg = elem;
  294. kp.flags = flags;
  295. *num = 0;
  296. /* We expect a comma-separated list of values. */
  297. do {
  298. int len;
  299. if (*num == max) {
  300. printk(KERN_ERR "%s: can only take %i arguments\n",
  301. name, max);
  302. return -EINVAL;
  303. }
  304. len = strcspn(val, ",");
  305. /* nul-terminate and parse */
  306. save = val[len];
  307. ((char *)val)[len] = '\0';
  308. ret = set(val, &kp);
  309. if (ret != 0)
  310. return ret;
  311. kp.arg += elemsize;
  312. val += len+1;
  313. (*num)++;
  314. } while (save == ',');
  315. if (*num < min) {
  316. printk(KERN_ERR "%s: needs at least %i arguments\n",
  317. name, min);
  318. return -EINVAL;
  319. }
  320. return 0;
  321. }
  322. static int param_array_set(const char *val, const struct kernel_param *kp)
  323. {
  324. const struct kparam_array *arr = kp->arr;
  325. unsigned int temp_num;
  326. return param_array(kp->name, val, 1, arr->max, arr->elem,
  327. arr->elemsize, arr->ops->set, kp->flags,
  328. arr->num ?: &temp_num);
  329. }
  330. static int param_array_get(char *buffer, const struct kernel_param *kp)
  331. {
  332. int i, off, ret;
  333. const struct kparam_array *arr = kp->arr;
  334. struct kernel_param p;
  335. p = *kp;
  336. for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
  337. if (i)
  338. buffer[off++] = ',';
  339. p.arg = arr->elem + arr->elemsize * i;
  340. ret = arr->ops->get(buffer + off, &p);
  341. if (ret < 0)
  342. return ret;
  343. off += ret;
  344. }
  345. buffer[off] = '\0';
  346. return off;
  347. }
  348. static void param_array_free(void *arg)
  349. {
  350. unsigned int i;
  351. const struct kparam_array *arr = arg;
  352. if (arr->ops->free)
  353. for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
  354. arr->ops->free(arr->elem + arr->elemsize * i);
  355. }
  356. struct kernel_param_ops param_array_ops = {
  357. .set = param_array_set,
  358. .get = param_array_get,
  359. .free = param_array_free,
  360. };
  361. EXPORT_SYMBOL(param_array_ops);
  362. int param_set_copystring(const char *val, const struct kernel_param *kp)
  363. {
  364. const struct kparam_string *kps = kp->str;
  365. if (strlen(val)+1 > kps->maxlen) {
  366. printk(KERN_ERR "%s: string doesn't fit in %u chars.\n",
  367. kp->name, kps->maxlen-1);
  368. return -ENOSPC;
  369. }
  370. strcpy(kps->string, val);
  371. return 0;
  372. }
  373. EXPORT_SYMBOL(param_set_copystring);
  374. int param_get_string(char *buffer, const struct kernel_param *kp)
  375. {
  376. const struct kparam_string *kps = kp->str;
  377. return strlcpy(buffer, kps->string, kps->maxlen);
  378. }
  379. EXPORT_SYMBOL(param_get_string);
  380. struct kernel_param_ops param_ops_string = {
  381. .set = param_set_copystring,
  382. .get = param_get_string,
  383. };
  384. EXPORT_SYMBOL(param_ops_string);
  385. /* sysfs output in /sys/modules/XYZ/parameters/ */
  386. #define to_module_attr(n) container_of(n, struct module_attribute, attr)
  387. #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
  388. extern struct kernel_param __start___param[], __stop___param[];
  389. struct param_attribute
  390. {
  391. struct module_attribute mattr;
  392. const struct kernel_param *param;
  393. };
  394. struct module_param_attrs
  395. {
  396. unsigned int num;
  397. struct attribute_group grp;
  398. struct param_attribute attrs[0];
  399. };
  400. #ifdef CONFIG_SYSFS
  401. #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
  402. static ssize_t param_attr_show(struct module_attribute *mattr,
  403. struct module *mod, char *buf)
  404. {
  405. int count;
  406. struct param_attribute *attribute = to_param_attr(mattr);
  407. if (!attribute->param->ops->get)
  408. return -EPERM;
  409. count = attribute->param->ops->get(buf, attribute->param);
  410. if (count > 0) {
  411. strcat(buf, "\n");
  412. ++count;
  413. }
  414. return count;
  415. }
  416. /* sysfs always hands a nul-terminated string in buf. We rely on that. */
  417. static ssize_t param_attr_store(struct module_attribute *mattr,
  418. struct module *owner,
  419. const char *buf, size_t len)
  420. {
  421. int err;
  422. struct param_attribute *attribute = to_param_attr(mattr);
  423. if (!attribute->param->ops->set)
  424. return -EPERM;
  425. err = attribute->param->ops->set(buf, attribute->param);
  426. if (!err)
  427. return len;
  428. return err;
  429. }
  430. #endif
  431. #ifdef CONFIG_MODULES
  432. #define __modinit
  433. #else
  434. #define __modinit __init
  435. #endif
  436. #ifdef CONFIG_SYSFS
  437. /*
  438. * add_sysfs_param - add a parameter to sysfs
  439. * @mk: struct module_kobject
  440. * @kparam: the actual parameter definition to add to sysfs
  441. * @name: name of parameter
  442. *
  443. * Create a kobject if for a (per-module) parameter if mp NULL, and
  444. * create file in sysfs. Returns an error on out of memory. Always cleans up
  445. * if there's an error.
  446. */
  447. static __modinit int add_sysfs_param(struct module_kobject *mk,
  448. const struct kernel_param *kp,
  449. const char *name)
  450. {
  451. struct module_param_attrs *new;
  452. struct attribute **attrs;
  453. int err, num;
  454. /* We don't bother calling this with invisible parameters. */
  455. BUG_ON(!kp->perm);
  456. if (!mk->mp) {
  457. num = 0;
  458. attrs = NULL;
  459. } else {
  460. num = mk->mp->num;
  461. attrs = mk->mp->grp.attrs;
  462. }
  463. /* Enlarge. */
  464. new = krealloc(mk->mp,
  465. sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),
  466. GFP_KERNEL);
  467. if (!new) {
  468. kfree(mk->mp);
  469. err = -ENOMEM;
  470. goto fail;
  471. }
  472. attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);
  473. if (!attrs) {
  474. err = -ENOMEM;
  475. goto fail_free_new;
  476. }
  477. /* Sysfs wants everything zeroed. */
  478. memset(new, 0, sizeof(*new));
  479. memset(&new->attrs[num], 0, sizeof(new->attrs[num]));
  480. memset(&attrs[num], 0, sizeof(attrs[num]));
  481. new->grp.name = "parameters";
  482. new->grp.attrs = attrs;
  483. /* Tack new one on the end. */
  484. sysfs_attr_init(&new->attrs[num].mattr.attr);
  485. new->attrs[num].param = kp;
  486. new->attrs[num].mattr.show = param_attr_show;
  487. new->attrs[num].mattr.store = param_attr_store;
  488. new->attrs[num].mattr.attr.name = (char *)name;
  489. new->attrs[num].mattr.attr.mode = kp->perm;
  490. new->num = num+1;
  491. /* Fix up all the pointers, since krealloc can move us */
  492. for (num = 0; num < new->num; num++)
  493. new->grp.attrs[num] = &new->attrs[num].mattr.attr;
  494. new->grp.attrs[num] = NULL;
  495. mk->mp = new;
  496. return 0;
  497. fail_free_new:
  498. kfree(new);
  499. fail:
  500. mk->mp = NULL;
  501. return err;
  502. }
  503. #ifdef CONFIG_MODULES
  504. static void free_module_param_attrs(struct module_kobject *mk)
  505. {
  506. kfree(mk->mp->grp.attrs);
  507. kfree(mk->mp);
  508. mk->mp = NULL;
  509. }
  510. /*
  511. * module_param_sysfs_setup - setup sysfs support for one module
  512. * @mod: module
  513. * @kparam: module parameters (array)
  514. * @num_params: number of module parameters
  515. *
  516. * Adds sysfs entries for module parameters under
  517. * /sys/module/[mod->name]/parameters/
  518. */
  519. int module_param_sysfs_setup(struct module *mod,
  520. const struct kernel_param *kparam,
  521. unsigned int num_params)
  522. {
  523. int i, err;
  524. bool params = false;
  525. for (i = 0; i < num_params; i++) {
  526. if (kparam[i].perm == 0)
  527. continue;
  528. err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
  529. if (err)
  530. return err;
  531. params = true;
  532. }
  533. if (!params)
  534. return 0;
  535. /* Create the param group. */
  536. err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
  537. if (err)
  538. free_module_param_attrs(&mod->mkobj);
  539. return err;
  540. }
  541. /*
  542. * module_param_sysfs_remove - remove sysfs support for one module
  543. * @mod: module
  544. *
  545. * Remove sysfs entries for module parameters and the corresponding
  546. * kobject.
  547. */
  548. void module_param_sysfs_remove(struct module *mod)
  549. {
  550. if (mod->mkobj.mp) {
  551. sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
  552. /* We are positive that no one is using any param
  553. * attrs at this point. Deallocate immediately. */
  554. free_module_param_attrs(&mod->mkobj);
  555. }
  556. }
  557. #endif
  558. void destroy_params(const struct kernel_param *params, unsigned num)
  559. {
  560. unsigned int i;
  561. for (i = 0; i < num; i++)
  562. if (params[i].ops->free)
  563. params[i].ops->free(params[i].arg);
  564. }
  565. static void __init kernel_add_sysfs_param(const char *name,
  566. struct kernel_param *kparam,
  567. unsigned int name_skip)
  568. {
  569. struct module_kobject *mk;
  570. struct kobject *kobj;
  571. int err;
  572. kobj = kset_find_obj(module_kset, name);
  573. if (kobj) {
  574. /* We already have one. Remove params so we can add more. */
  575. mk = to_module_kobject(kobj);
  576. /* We need to remove it before adding parameters. */
  577. sysfs_remove_group(&mk->kobj, &mk->mp->grp);
  578. } else {
  579. mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
  580. BUG_ON(!mk);
  581. mk->mod = THIS_MODULE;
  582. mk->kobj.kset = module_kset;
  583. err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
  584. "%s", name);
  585. if (err) {
  586. kobject_put(&mk->kobj);
  587. printk(KERN_ERR "Module '%s' failed add to sysfs, "
  588. "error number %d\n", name, err);
  589. printk(KERN_ERR "The system will be unstable now.\n");
  590. return;
  591. }
  592. /* So that exit path is even. */
  593. kobject_get(&mk->kobj);
  594. }
  595. /* These should not fail at boot. */
  596. err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
  597. BUG_ON(err);
  598. err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
  599. BUG_ON(err);
  600. kobject_uevent(&mk->kobj, KOBJ_ADD);
  601. kobject_put(&mk->kobj);
  602. }
  603. /*
  604. * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
  605. *
  606. * Add module_parameters to sysfs for "modules" built into the kernel.
  607. *
  608. * The "module" name (KBUILD_MODNAME) is stored before a dot, the
  609. * "parameter" name is stored behind a dot in kernel_param->name. So,
  610. * extract the "module" name for all built-in kernel_param-eters,
  611. * and for all who have the same, call kernel_add_sysfs_param.
  612. */
  613. static void __init param_sysfs_builtin(void)
  614. {
  615. struct kernel_param *kp;
  616. unsigned int name_len;
  617. char modname[MODULE_NAME_LEN];
  618. for (kp = __start___param; kp < __stop___param; kp++) {
  619. char *dot;
  620. if (kp->perm == 0)
  621. continue;
  622. dot = strchr(kp->name, '.');
  623. if (!dot) {
  624. /* This happens for core_param() */
  625. strcpy(modname, "kernel");
  626. name_len = 0;
  627. } else {
  628. name_len = dot - kp->name + 1;
  629. strlcpy(modname, kp->name, name_len);
  630. }
  631. kernel_add_sysfs_param(modname, kp, name_len);
  632. }
  633. }
  634. /* module-related sysfs stuff */
  635. static ssize_t module_attr_show(struct kobject *kobj,
  636. struct attribute *attr,
  637. char *buf)
  638. {
  639. struct module_attribute *attribute;
  640. struct module_kobject *mk;
  641. int ret;
  642. attribute = to_module_attr(attr);
  643. mk = to_module_kobject(kobj);
  644. if (!attribute->show)
  645. return -EIO;
  646. ret = attribute->show(attribute, mk->mod, buf);
  647. return ret;
  648. }
  649. static ssize_t module_attr_store(struct kobject *kobj,
  650. struct attribute *attr,
  651. const char *buf, size_t len)
  652. {
  653. struct module_attribute *attribute;
  654. struct module_kobject *mk;
  655. int ret;
  656. attribute = to_module_attr(attr);
  657. mk = to_module_kobject(kobj);
  658. if (!attribute->store)
  659. return -EIO;
  660. ret = attribute->store(attribute, mk->mod, buf, len);
  661. return ret;
  662. }
  663. static const struct sysfs_ops module_sysfs_ops = {
  664. .show = module_attr_show,
  665. .store = module_attr_store,
  666. };
  667. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  668. {
  669. struct kobj_type *ktype = get_ktype(kobj);
  670. if (ktype == &module_ktype)
  671. return 1;
  672. return 0;
  673. }
  674. static const struct kset_uevent_ops module_uevent_ops = {
  675. .filter = uevent_filter,
  676. };
  677. struct kset *module_kset;
  678. int module_sysfs_initialized;
  679. struct kobj_type module_ktype = {
  680. .sysfs_ops = &module_sysfs_ops,
  681. };
  682. /*
  683. * param_sysfs_init - wrapper for built-in params support
  684. */
  685. static int __init param_sysfs_init(void)
  686. {
  687. module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
  688. if (!module_kset) {
  689. printk(KERN_WARNING "%s (%d): error creating kset\n",
  690. __FILE__, __LINE__);
  691. return -ENOMEM;
  692. }
  693. module_sysfs_initialized = 1;
  694. param_sysfs_builtin();
  695. return 0;
  696. }
  697. subsys_initcall(param_sysfs_init);
  698. #endif /* CONFIG_SYSFS */