params.c 22 KB

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