cifsroot.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SMB root file system support
  4. *
  5. * Copyright (c) 2019 Paulo Alcantara <[email protected]>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/fs.h>
  9. #include <linux/types.h>
  10. #include <linux/ctype.h>
  11. #include <linux/string.h>
  12. #include <linux/root_dev.h>
  13. #include <linux/kernel.h>
  14. #include <linux/in.h>
  15. #include <linux/inet.h>
  16. #include <net/ipconfig.h>
  17. #define DEFAULT_MNT_OPTS \
  18. "vers=1.0,cifsacl,mfsymlinks,rsize=1048576,wsize=65536,uid=0,gid=0," \
  19. "hard,rootfs"
  20. static char root_dev[2048] __initdata = "";
  21. static char root_opts[1024] __initdata = DEFAULT_MNT_OPTS;
  22. static __be32 __init parse_srvaddr(char *start, char *end)
  23. {
  24. /* TODO: ipv6 support */
  25. char addr[sizeof("aaa.bbb.ccc.ddd")];
  26. int i = 0;
  27. while (start < end && i < sizeof(addr) - 1) {
  28. if (isdigit(*start) || *start == '.')
  29. addr[i++] = *start;
  30. start++;
  31. }
  32. addr[i] = '\0';
  33. return in_aton(addr);
  34. }
  35. /* cifsroot=//<server-ip>/<share>[,options] */
  36. static int __init cifs_root_setup(char *line)
  37. {
  38. char *s;
  39. int len;
  40. __be32 srvaddr = htonl(INADDR_NONE);
  41. ROOT_DEV = Root_CIFS;
  42. if (strlen(line) > 3 && line[0] == '/' && line[1] == '/') {
  43. s = strchr(&line[2], '/');
  44. if (!s || s[1] == '\0')
  45. return 1;
  46. /* make s point to ',' or '\0' at end of line */
  47. s = strchrnul(s, ',');
  48. /* len is strlen(unc) + '\0' */
  49. len = s - line + 1;
  50. if (len > sizeof(root_dev)) {
  51. pr_err("Root-CIFS: UNC path too long\n");
  52. return 1;
  53. }
  54. strscpy(root_dev, line, len);
  55. srvaddr = parse_srvaddr(&line[2], s);
  56. if (*s) {
  57. int n = snprintf(root_opts,
  58. sizeof(root_opts), "%s,%s",
  59. DEFAULT_MNT_OPTS, s + 1);
  60. if (n >= sizeof(root_opts)) {
  61. pr_err("Root-CIFS: mount options string too long\n");
  62. root_opts[sizeof(root_opts)-1] = '\0';
  63. return 1;
  64. }
  65. }
  66. }
  67. root_server_addr = srvaddr;
  68. return 1;
  69. }
  70. __setup("cifsroot=", cifs_root_setup);
  71. int __init cifs_root_data(char **dev, char **opts)
  72. {
  73. if (!root_dev[0] || root_server_addr == htonl(INADDR_NONE)) {
  74. pr_err("Root-CIFS: no SMB server address\n");
  75. return -1;
  76. }
  77. *dev = root_dev;
  78. *opts = root_opts;
  79. return 0;
  80. }