params.c 22 KB

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