params.c 22 KB

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