params.c 22 KB

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