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