params.c 20 KB

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