params.c 21 KB

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