params.c 22 KB

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