params.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/errno.h>
  19. #include <linux/module.h>
  20. #include <linux/device.h>
  21. #include <linux/err.h>
  22. #include <linux/slab.h>
  23. #include <linux/ctype.h>
  24. /* Protects all parameters, and incidentally kmalloced_param list. */
  25. static DEFINE_MUTEX(param_lock);
  26. /* This just allows us to keep track of which parameters are kmalloced. */
  27. struct kmalloced_param {
  28. struct list_head list;
  29. char val[];
  30. };
  31. static LIST_HEAD(kmalloced_params);
  32. static void *kmalloc_parameter(unsigned int size)
  33. {
  34. struct kmalloced_param *p;
  35. p = kmalloc(sizeof(*p) + size, GFP_KERNEL);
  36. if (!p)
  37. return NULL;
  38. list_add(&p->list, &kmalloced_params);
  39. return p->val;
  40. }
  41. /* Does nothing if parameter wasn't kmalloced above. */
  42. static void maybe_kfree_parameter(void *param)
  43. {
  44. struct kmalloced_param *p;
  45. list_for_each_entry(p, &kmalloced_params, list) {
  46. if (p->val == param) {
  47. list_del(&p->list);
  48. kfree(p);
  49. break;
  50. }
  51. }
  52. }
  53. static char dash2underscore(char c)
  54. {
  55. if (c == '-')
  56. return '_';
  57. return c;
  58. }
  59. bool parameqn(const char *a, const char *b, size_t n)
  60. {
  61. size_t i;
  62. for (i = 0; i < n; i++) {
  63. if (dash2underscore(a[i]) != dash2underscore(b[i]))
  64. return false;
  65. }
  66. return true;
  67. }
  68. bool parameq(const char *a, const char *b)
  69. {
  70. return parameqn(a, b, strlen(a)+1);
  71. }
  72. static int parse_one(char *param,
  73. char *val,
  74. const struct kernel_param *params,
  75. unsigned num_params,
  76. int (*handle_unknown)(char *param, char *val))
  77. {
  78. unsigned int i;
  79. int err;
  80. /* Find parameter */
  81. for (i = 0; i < num_params; i++) {
  82. if (parameq(param, params[i].name)) {
  83. /* No one handled NULL, so do it here. */
  84. if (!val && params[i].ops->set != param_set_bool
  85. && params[i].ops->set != param_set_bint)
  86. return -EINVAL;
  87. pr_debug("They are equal! Calling %p\n",
  88. params[i].ops->set);
  89. mutex_lock(&param_lock);
  90. err = params[i].ops->set(val, &params[i]);
  91. mutex_unlock(&param_lock);
  92. return err;
  93. }
  94. }
  95. if (handle_unknown) {
  96. pr_debug("Unknown argument: calling %p\n", handle_unknown);
  97. return handle_unknown(param, val);
  98. }
  99. pr_debug("Unknown argument `%s'\n", param);
  100. return -ENOENT;
  101. }
  102. /* You can use " around spaces, but can't escape ". */
  103. /* Hyphens and underscores equivalent in parameter names. */
  104. static char *next_arg(char *args, char **param, char **val)
  105. {
  106. unsigned int i, equals = 0;
  107. int in_quote = 0, quoted = 0;
  108. char *next;
  109. if (*args == '"') {
  110. args++;
  111. in_quote = 1;
  112. quoted = 1;
  113. }
  114. for (i = 0; args[i]; i++) {
  115. if (isspace(args[i]) && !in_quote)
  116. break;
  117. if (equals == 0) {
  118. if (args[i] == '=')
  119. equals = i;
  120. }
  121. if (args[i] == '"')
  122. in_quote = !in_quote;
  123. }
  124. *param = args;
  125. if (!equals)
  126. *val = NULL;
  127. else {
  128. args[equals] = '\0';
  129. *val = args + equals + 1;
  130. /* Don't include quotes in value. */
  131. if (**val == '"') {
  132. (*val)++;
  133. if (args[i-1] == '"')
  134. args[i-1] = '\0';
  135. }
  136. if (quoted && args[i-1] == '"')
  137. args[i-1] = '\0';
  138. }
  139. if (args[i]) {
  140. args[i] = '\0';
  141. next = args + i + 1;
  142. } else
  143. next = args + i;
  144. /* Chew up trailing spaces. */
  145. return skip_spaces(next);
  146. }
  147. /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
  148. int parse_args(const char *name,
  149. char *args,
  150. const struct kernel_param *params,
  151. unsigned num,
  152. int (*unknown)(char *param, char *val))
  153. {
  154. char *param, *val;
  155. pr_debug("Parsing ARGS: %s\n", args);
  156. /* Chew leading spaces */
  157. args = skip_spaces(args);
  158. while (*args) {
  159. int ret;
  160. int irq_was_disabled;
  161. args = next_arg(args, &param, &val);
  162. irq_was_disabled = irqs_disabled();
  163. ret = parse_one(param, val, params, num, unknown);
  164. if (irq_was_disabled && !irqs_disabled()) {
  165. printk(KERN_WARNING "parse_args(): option '%s' enabled "
  166. "irq's!\n", param);
  167. }
  168. switch (ret) {
  169. case -ENOENT:
  170. printk(KERN_ERR "%s: Unknown parameter `%s'\n",
  171. name, param);
  172. return ret;
  173. case -ENOSPC:
  174. printk(KERN_ERR
  175. "%s: `%s' too large for parameter `%s'\n",
  176. name, val ?: "", param);
  177. return ret;
  178. case 0:
  179. break;
  180. default:
  181. printk(KERN_ERR
  182. "%s: `%s' invalid for parameter `%s'\n",
  183. name, val ?: "", param);
  184. return ret;
  185. }
  186. }
  187. /* All parsed OK. */
  188. return 0;
  189. }
  190. /* Lazy bastard, eh? */
  191. #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
  192. int param_set_##name(const char *val, const struct kernel_param *kp) \
  193. { \
  194. tmptype l; \
  195. int ret; \
  196. \
  197. ret = strtolfn(val, 0, &l); \
  198. if (ret < 0 || ((type)l != l)) \
  199. return ret < 0 ? ret : -EINVAL; \
  200. *((type *)kp->arg) = l; \
  201. return 0; \
  202. } \
  203. int param_get_##name(char *buffer, const struct kernel_param *kp) \
  204. { \
  205. return sprintf(buffer, format, *((type *)kp->arg)); \
  206. } \
  207. struct kernel_param_ops param_ops_##name = { \
  208. .set = param_set_##name, \
  209. .get = param_get_##name, \
  210. }; \
  211. EXPORT_SYMBOL(param_set_##name); \
  212. EXPORT_SYMBOL(param_get_##name); \
  213. EXPORT_SYMBOL(param_ops_##name)
  214. STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
  215. STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
  216. STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul);
  217. STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol);
  218. STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, strict_strtoul);
  219. STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol);
  220. STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, strict_strtoul);
  221. int param_set_charp(const char *val, const struct kernel_param *kp)
  222. {
  223. if (strlen(val) > 1024) {
  224. printk(KERN_ERR "%s: string parameter too long\n",
  225. kp->name);
  226. return -ENOSPC;
  227. }
  228. maybe_kfree_parameter(*(char **)kp->arg);
  229. /* This is a hack. We can't kmalloc in early boot, and we
  230. * don't need to; this mangled commandline is preserved. */
  231. if (slab_is_available()) {
  232. *(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
  233. if (!*(char **)kp->arg)
  234. return -ENOMEM;
  235. strcpy(*(char **)kp->arg, val);
  236. } else
  237. *(const char **)kp->arg = val;
  238. return 0;
  239. }
  240. EXPORT_SYMBOL(param_set_charp);
  241. int param_get_charp(char *buffer, const struct kernel_param *kp)
  242. {
  243. return sprintf(buffer, "%s", *((char **)kp->arg));
  244. }
  245. EXPORT_SYMBOL(param_get_charp);
  246. static void param_free_charp(void *arg)
  247. {
  248. maybe_kfree_parameter(*((char **)arg));
  249. }
  250. struct kernel_param_ops param_ops_charp = {
  251. .set = param_set_charp,
  252. .get = param_get_charp,
  253. .free = param_free_charp,
  254. };
  255. EXPORT_SYMBOL(param_ops_charp);
  256. /* Actually could be a bool or an int, for historical reasons. */
  257. int param_set_bool(const char *val, const struct kernel_param *kp)
  258. {
  259. bool v;
  260. int ret;
  261. /* No equals means "set"... */
  262. if (!val) val = "1";
  263. /* One of =[yYnN01] */
  264. ret = strtobool(val, &v);
  265. if (ret)
  266. return ret;
  267. if (kp->flags & KPARAM_ISBOOL)
  268. *(bool *)kp->arg = v;
  269. else
  270. *(int *)kp->arg = v;
  271. return 0;
  272. }
  273. EXPORT_SYMBOL(param_set_bool);
  274. int param_get_bool(char *buffer, const struct kernel_param *kp)
  275. {
  276. bool val;
  277. if (kp->flags & KPARAM_ISBOOL)
  278. val = *(bool *)kp->arg;
  279. else
  280. val = *(int *)kp->arg;
  281. /* Y and N chosen as being relatively non-coder friendly */
  282. return sprintf(buffer, "%c", val ? 'Y' : 'N');
  283. }
  284. EXPORT_SYMBOL(param_get_bool);
  285. struct kernel_param_ops param_ops_bool = {
  286. .set = param_set_bool,
  287. .get = param_get_bool,
  288. };
  289. EXPORT_SYMBOL(param_ops_bool);
  290. /* This one must be bool. */
  291. int param_set_invbool(const char *val, const struct kernel_param *kp)
  292. {
  293. int ret;
  294. bool boolval;
  295. struct kernel_param dummy;
  296. dummy.arg = &boolval;
  297. dummy.flags = KPARAM_ISBOOL;
  298. ret = param_set_bool(val, &dummy);
  299. if (ret == 0)
  300. *(bool *)kp->arg = !boolval;
  301. return ret;
  302. }
  303. EXPORT_SYMBOL(param_set_invbool);
  304. int param_get_invbool(char *buffer, const struct kernel_param *kp)
  305. {
  306. return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y');
  307. }
  308. EXPORT_SYMBOL(param_get_invbool);
  309. struct kernel_param_ops param_ops_invbool = {
  310. .set = param_set_invbool,
  311. .get = param_get_invbool,
  312. };
  313. EXPORT_SYMBOL(param_ops_invbool);
  314. int param_set_bint(const char *val, const struct kernel_param *kp)
  315. {
  316. struct kernel_param boolkp;
  317. bool v;
  318. int ret;
  319. /* Match bool exactly, by re-using it. */
  320. boolkp = *kp;
  321. boolkp.arg = &v;
  322. boolkp.flags |= KPARAM_ISBOOL;
  323. ret = param_set_bool(val, &boolkp);
  324. if (ret == 0)
  325. *(int *)kp->arg = v;
  326. return ret;
  327. }
  328. EXPORT_SYMBOL(param_set_bint);
  329. struct kernel_param_ops param_ops_bint = {
  330. .set = param_set_bint,
  331. .get = param_get_int,
  332. };
  333. EXPORT_SYMBOL(param_ops_bint);
  334. /* We break the rule and mangle the string. */
  335. static int param_array(const char *name,
  336. const char *val,
  337. unsigned int min, unsigned int max,
  338. void *elem, int elemsize,
  339. int (*set)(const char *, const struct kernel_param *kp),
  340. u16 flags,
  341. unsigned int *num)
  342. {
  343. int ret;
  344. struct kernel_param kp;
  345. char save;
  346. /* Get the name right for errors. */
  347. kp.name = name;
  348. kp.arg = elem;
  349. kp.flags = flags;
  350. *num = 0;
  351. /* We expect a comma-separated list of values. */
  352. do {
  353. int len;
  354. if (*num == max) {
  355. printk(KERN_ERR "%s: can only take %i arguments\n",
  356. name, max);
  357. return -EINVAL;
  358. }
  359. len = strcspn(val, ",");
  360. /* nul-terminate and parse */
  361. save = val[len];
  362. ((char *)val)[len] = '\0';
  363. BUG_ON(!mutex_is_locked(&param_lock));
  364. ret = set(val, &kp);
  365. if (ret != 0)
  366. return ret;
  367. kp.arg += elemsize;
  368. val += len+1;
  369. (*num)++;
  370. } while (save == ',');
  371. if (*num < min) {
  372. printk(KERN_ERR "%s: needs at least %i arguments\n",
  373. name, min);
  374. return -EINVAL;
  375. }
  376. return 0;
  377. }
  378. static int param_array_set(const char *val, const struct kernel_param *kp)
  379. {
  380. const struct kparam_array *arr = kp->arr;
  381. unsigned int temp_num;
  382. return param_array(kp->name, val, 1, arr->max, arr->elem,
  383. arr->elemsize, arr->ops->set, kp->flags,
  384. arr->num ?: &temp_num);
  385. }
  386. static int param_array_get(char *buffer, const struct kernel_param *kp)
  387. {
  388. int i, off, ret;
  389. const struct kparam_array *arr = kp->arr;
  390. struct kernel_param p;
  391. p = *kp;
  392. for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
  393. if (i)
  394. buffer[off++] = ',';
  395. p.arg = arr->elem + arr->elemsize * i;
  396. BUG_ON(!mutex_is_locked(&param_lock));
  397. ret = arr->ops->get(buffer + off, &p);
  398. if (ret < 0)
  399. return ret;
  400. off += ret;
  401. }
  402. buffer[off] = '\0';
  403. return off;
  404. }
  405. static void param_array_free(void *arg)
  406. {
  407. unsigned int i;
  408. const struct kparam_array *arr = arg;
  409. if (arr->ops->free)
  410. for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
  411. arr->ops->free(arr->elem + arr->elemsize * i);
  412. }
  413. struct kernel_param_ops param_array_ops = {
  414. .set = param_array_set,
  415. .get = param_array_get,
  416. .free = param_array_free,
  417. };
  418. EXPORT_SYMBOL(param_array_ops);
  419. int param_set_copystring(const char *val, const struct kernel_param *kp)
  420. {
  421. const struct kparam_string *kps = kp->str;
  422. if (strlen(val)+1 > kps->maxlen) {
  423. printk(KERN_ERR "%s: string doesn't fit in %u chars.\n",
  424. kp->name, kps->maxlen-1);
  425. return -ENOSPC;
  426. }
  427. strcpy(kps->string, val);
  428. return 0;
  429. }
  430. EXPORT_SYMBOL(param_set_copystring);
  431. int param_get_string(char *buffer, const struct kernel_param *kp)
  432. {
  433. const struct kparam_string *kps = kp->str;
  434. return strlcpy(buffer, kps->string, kps->maxlen);
  435. }
  436. EXPORT_SYMBOL(param_get_string);
  437. struct kernel_param_ops param_ops_string = {
  438. .set = param_set_copystring,
  439. .get = param_get_string,
  440. };
  441. EXPORT_SYMBOL(param_ops_string);
  442. /* sysfs output in /sys/modules/XYZ/parameters/ */
  443. #define to_module_attr(n) container_of(n, struct module_attribute, attr)
  444. #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
  445. extern struct kernel_param __start___param[], __stop___param[];
  446. struct param_attribute
  447. {
  448. struct module_attribute mattr;
  449. const struct kernel_param *param;
  450. };
  451. struct module_param_attrs
  452. {
  453. unsigned int num;
  454. struct attribute_group grp;
  455. struct param_attribute attrs[0];
  456. };
  457. #ifdef CONFIG_SYSFS
  458. #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
  459. static ssize_t param_attr_show(struct module_attribute *mattr,
  460. struct module_kobject *mk, char *buf)
  461. {
  462. int count;
  463. struct param_attribute *attribute = to_param_attr(mattr);
  464. if (!attribute->param->ops->get)
  465. return -EPERM;
  466. mutex_lock(&param_lock);
  467. count = attribute->param->ops->get(buf, attribute->param);
  468. mutex_unlock(&param_lock);
  469. if (count > 0) {
  470. strcat(buf, "\n");
  471. ++count;
  472. }
  473. return count;
  474. }
  475. /* sysfs always hands a nul-terminated string in buf. We rely on that. */
  476. static ssize_t param_attr_store(struct module_attribute *mattr,
  477. struct module_kobject *km,
  478. const char *buf, size_t len)
  479. {
  480. int err;
  481. struct param_attribute *attribute = to_param_attr(mattr);
  482. if (!attribute->param->ops->set)
  483. return -EPERM;
  484. mutex_lock(&param_lock);
  485. err = attribute->param->ops->set(buf, attribute->param);
  486. mutex_unlock(&param_lock);
  487. if (!err)
  488. return len;
  489. return err;
  490. }
  491. #endif
  492. #ifdef CONFIG_MODULES
  493. #define __modinit
  494. #else
  495. #define __modinit __init
  496. #endif
  497. #ifdef CONFIG_SYSFS
  498. void __kernel_param_lock(void)
  499. {
  500. mutex_lock(&param_lock);
  501. }
  502. EXPORT_SYMBOL(__kernel_param_lock);
  503. void __kernel_param_unlock(void)
  504. {
  505. mutex_unlock(&param_lock);
  506. }
  507. EXPORT_SYMBOL(__kernel_param_unlock);
  508. /*
  509. * add_sysfs_param - add a parameter to sysfs
  510. * @mk: struct module_kobject
  511. * @kparam: the actual parameter definition to add to sysfs
  512. * @name: name of parameter
  513. *
  514. * Create a kobject if for a (per-module) parameter if mp NULL, and
  515. * create file in sysfs. Returns an error on out of memory. Always cleans up
  516. * if there's an error.
  517. */
  518. static __modinit int add_sysfs_param(struct module_kobject *mk,
  519. const struct kernel_param *kp,
  520. const char *name)
  521. {
  522. struct module_param_attrs *new;
  523. struct attribute **attrs;
  524. int err, num;
  525. /* We don't bother calling this with invisible parameters. */
  526. BUG_ON(!kp->perm);
  527. if (!mk->mp) {
  528. num = 0;
  529. attrs = NULL;
  530. } else {
  531. num = mk->mp->num;
  532. attrs = mk->mp->grp.attrs;
  533. }
  534. /* Enlarge. */
  535. new = krealloc(mk->mp,
  536. sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),
  537. GFP_KERNEL);
  538. if (!new) {
  539. kfree(mk->mp);
  540. err = -ENOMEM;
  541. goto fail;
  542. }
  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. printk(KERN_ERR
  658. "Module '%s' failed add to sysfs, error number %d\n",
  659. name, err);
  660. printk(KERN_ERR
  661. "The system will be unstable now.\n");
  662. return NULL;
  663. }
  664. /* So that we hold reference in both cases. */
  665. kobject_get(&mk->kobj);
  666. }
  667. return mk;
  668. }
  669. static void __init kernel_add_sysfs_param(const char *name,
  670. struct kernel_param *kparam,
  671. unsigned int name_skip)
  672. {
  673. struct module_kobject *mk;
  674. int err;
  675. mk = locate_module_kobject(name);
  676. if (!mk)
  677. return;
  678. /* We need to remove old parameters before adding more. */
  679. if (mk->mp)
  680. sysfs_remove_group(&mk->kobj, &mk->mp->grp);
  681. /* These should not fail at boot. */
  682. err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
  683. BUG_ON(err);
  684. err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
  685. BUG_ON(err);
  686. kobject_uevent(&mk->kobj, KOBJ_ADD);
  687. kobject_put(&mk->kobj);
  688. }
  689. /*
  690. * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
  691. *
  692. * Add module_parameters to sysfs for "modules" built into the kernel.
  693. *
  694. * The "module" name (KBUILD_MODNAME) is stored before a dot, the
  695. * "parameter" name is stored behind a dot in kernel_param->name. So,
  696. * extract the "module" name for all built-in kernel_param-eters,
  697. * and for all who have the same, call kernel_add_sysfs_param.
  698. */
  699. static void __init param_sysfs_builtin(void)
  700. {
  701. struct kernel_param *kp;
  702. unsigned int name_len;
  703. char modname[MODULE_NAME_LEN];
  704. for (kp = __start___param; kp < __stop___param; kp++) {
  705. char *dot;
  706. if (kp->perm == 0)
  707. continue;
  708. dot = strchr(kp->name, '.');
  709. if (!dot) {
  710. /* This happens for core_param() */
  711. strcpy(modname, "kernel");
  712. name_len = 0;
  713. } else {
  714. name_len = dot - kp->name + 1;
  715. strlcpy(modname, kp->name, name_len);
  716. }
  717. kernel_add_sysfs_param(modname, kp, name_len);
  718. }
  719. }
  720. ssize_t __modver_version_show(struct module_attribute *mattr,
  721. struct module_kobject *mk, char *buf)
  722. {
  723. struct module_version_attribute *vattr =
  724. container_of(mattr, struct module_version_attribute, mattr);
  725. return sprintf(buf, "%s\n", vattr->version);
  726. }
  727. extern const struct module_version_attribute *__start___modver[];
  728. extern const struct module_version_attribute *__stop___modver[];
  729. static void __init version_sysfs_builtin(void)
  730. {
  731. const struct module_version_attribute **p;
  732. struct module_kobject *mk;
  733. int err;
  734. for (p = __start___modver; p < __stop___modver; p++) {
  735. const struct module_version_attribute *vattr = *p;
  736. mk = locate_module_kobject(vattr->module_name);
  737. if (mk) {
  738. err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
  739. kobject_uevent(&mk->kobj, KOBJ_ADD);
  740. kobject_put(&mk->kobj);
  741. }
  742. }
  743. }
  744. /* module-related sysfs stuff */
  745. static ssize_t module_attr_show(struct kobject *kobj,
  746. struct attribute *attr,
  747. char *buf)
  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->show)
  755. return -EIO;
  756. ret = attribute->show(attribute, mk, buf);
  757. return ret;
  758. }
  759. static ssize_t module_attr_store(struct kobject *kobj,
  760. struct attribute *attr,
  761. const char *buf, size_t len)
  762. {
  763. struct module_attribute *attribute;
  764. struct module_kobject *mk;
  765. int ret;
  766. attribute = to_module_attr(attr);
  767. mk = to_module_kobject(kobj);
  768. if (!attribute->store)
  769. return -EIO;
  770. ret = attribute->store(attribute, mk, buf, len);
  771. return ret;
  772. }
  773. static const struct sysfs_ops module_sysfs_ops = {
  774. .show = module_attr_show,
  775. .store = module_attr_store,
  776. };
  777. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  778. {
  779. struct kobj_type *ktype = get_ktype(kobj);
  780. if (ktype == &module_ktype)
  781. return 1;
  782. return 0;
  783. }
  784. static const struct kset_uevent_ops module_uevent_ops = {
  785. .filter = uevent_filter,
  786. };
  787. struct kset *module_kset;
  788. int module_sysfs_initialized;
  789. struct kobj_type module_ktype = {
  790. .sysfs_ops = &module_sysfs_ops,
  791. };
  792. /*
  793. * param_sysfs_init - wrapper for built-in params support
  794. */
  795. static int __init param_sysfs_init(void)
  796. {
  797. module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
  798. if (!module_kset) {
  799. printk(KERN_WARNING "%s (%d): error creating kset\n",
  800. __FILE__, __LINE__);
  801. return -ENOMEM;
  802. }
  803. module_sysfs_initialized = 1;
  804. version_sysfs_builtin();
  805. param_sysfs_builtin();
  806. return 0;
  807. }
  808. subsys_initcall(param_sysfs_init);
  809. #endif /* CONFIG_SYSFS */