blob: 9e91a5a40aaec21fd31ebfa6b0a6df70ad9ae30c [file] [log] [blame]
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03001// SPDX-License-Identifier: GPL-2.0
2/*
3 * SMB root file system support
4 *
5 * Copyright (c) 2019 Paulo Alcantara <palcantara@suse.de>
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
18#define DEFAULT_MNT_OPTS \
19 "vers=1.0,cifsacl,mfsymlinks,rsize=1048576,wsize=65536,uid=0,gid=0," \
20 "hard,rootfs"
21
22static char root_dev[2048] __initdata = "";
23static char root_opts[1024] __initdata = DEFAULT_MNT_OPTS;
24
25static __be32 __init parse_srvaddr(char *start, char *end)
26{
Aurelien Aptel352f2c92019-09-16 05:45:42 +020027 /* TODO: ipv6 support */
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -030028 char addr[sizeof("aaa.bbb.ccc.ddd")];
29 int i = 0;
30
31 while (start < end && i < sizeof(addr) - 1) {
32 if (isdigit(*start) || *start == '.')
33 addr[i++] = *start;
34 start++;
35 }
36 addr[i] = '\0';
37 return in_aton(addr);
38}
39
40/* cifsroot=//<server-ip>/<share>[,options] */
41static int __init cifs_root_setup(char *line)
42{
43 char *s;
44 int len;
45 __be32 srvaddr = htonl(INADDR_NONE);
46
47 ROOT_DEV = Root_CIFS;
48
49 if (strlen(line) > 3 && line[0] == '/' && line[1] == '/') {
50 s = strchr(&line[2], '/');
51 if (!s || s[1] == '\0')
52 return 1;
53
Aurelien Aptel352f2c92019-09-16 05:45:42 +020054 /* make s point to ',' or '\0' at end of line */
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -030055 s = strchrnul(s, ',');
Aurelien Aptel352f2c92019-09-16 05:45:42 +020056 /* len is strlen(unc) + '\0' */
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -030057 len = s - line + 1;
Aurelien Aptel352f2c92019-09-16 05:45:42 +020058 if (len > sizeof(root_dev)) {
Joe Perchesa0a30362020-04-14 22:42:53 -070059 pr_err("Root-CIFS: UNC path too long\n");
Aurelien Aptel352f2c92019-09-16 05:45:42 +020060 return 1;
61 }
62 strlcpy(root_dev, line, len);
63 srvaddr = parse_srvaddr(&line[2], s);
64 if (*s) {
65 int n = snprintf(root_opts,
66 sizeof(root_opts), "%s,%s",
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -030067 DEFAULT_MNT_OPTS, s + 1);
Aurelien Aptel352f2c92019-09-16 05:45:42 +020068 if (n >= sizeof(root_opts)) {
Joe Perchesa0a30362020-04-14 22:42:53 -070069 pr_err("Root-CIFS: mount options string too long\n");
Aurelien Aptel352f2c92019-09-16 05:45:42 +020070 root_opts[sizeof(root_opts)-1] = '\0';
71 return 1;
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -030072 }
73 }
74 }
75
76 root_server_addr = srvaddr;
77
78 return 1;
79}
80
81__setup("cifsroot=", cifs_root_setup);
82
83int __init cifs_root_data(char **dev, char **opts)
84{
85 if (!root_dev[0] || root_server_addr == htonl(INADDR_NONE)) {
Joe Perchesa0a30362020-04-14 22:42:53 -070086 pr_err("Root-CIFS: no SMB server address\n");
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -030087 return -1;
88 }
89
90 *dev = root_dev;
91 *opts = root_opts;
92
93 return 0;
94}