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