0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074 #include <linux/types.h>
0075 #include <linux/string.h>
0076 #include <linux/init.h>
0077 #include <linux/nfs.h>
0078 #include <linux/nfs_fs.h>
0079 #include <linux/utsname.h>
0080 #include <linux/root_dev.h>
0081 #include <net/ipconfig.h>
0082
0083 #include "internal.h"
0084
0085 #define NFSDBG_FACILITY NFSDBG_ROOT
0086
0087
0088 #define NFS_ROOT "/tftpboot/%s"
0089
0090
0091 #if defined(CONFIG_NFS_V2)
0092 #define NFS_DEF_OPTIONS "vers=2,tcp,rsize=4096,wsize=4096"
0093 #elif defined(CONFIG_NFS_V3)
0094 #define NFS_DEF_OPTIONS "vers=3,tcp,rsize=4096,wsize=4096"
0095 #else
0096 #define NFS_DEF_OPTIONS "vers=4,tcp,rsize=4096,wsize=4096"
0097 #endif
0098
0099
0100 static char nfs_root_parms[NFS_MAXPATHLEN + 1] __initdata = "";
0101
0102
0103 static char nfs_root_options[256] __initdata = NFS_DEF_OPTIONS;
0104
0105
0106 static __be32 servaddr __initdata = htonl(INADDR_NONE);
0107
0108
0109 static char nfs_export_path[NFS_MAXPATHLEN + 1] __initdata = "";
0110
0111
0112 static char nfs_root_device[NFS_MAXPATHLEN + 1] __initdata = "";
0113
0114 #ifdef NFS_DEBUG
0115
0116
0117
0118
0119 static int __init nfs_root_debug(char *__unused)
0120 {
0121 nfs_debug |= NFSDBG_ROOT | NFSDBG_MOUNT;
0122 return 1;
0123 }
0124
0125 __setup("nfsrootdebug", nfs_root_debug);
0126 #endif
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137 static int __init nfs_root_setup(char *line)
0138 {
0139 ROOT_DEV = Root_NFS;
0140
0141 if (line[0] == '/' || line[0] == ',' || (line[0] >= '0' && line[0] <= '9')) {
0142 strlcpy(nfs_root_parms, line, sizeof(nfs_root_parms));
0143 } else {
0144 size_t n = strlen(line) + sizeof(NFS_ROOT) - 1;
0145 if (n >= sizeof(nfs_root_parms))
0146 line[sizeof(nfs_root_parms) - sizeof(NFS_ROOT) - 2] = '\0';
0147 sprintf(nfs_root_parms, NFS_ROOT, line);
0148 }
0149
0150
0151
0152
0153
0154
0155
0156
0157 root_server_addr = root_nfs_parse_addr(nfs_root_parms);
0158
0159 return 1;
0160 }
0161
0162 __setup("nfsroot=", nfs_root_setup);
0163
0164 static int __init root_nfs_copy(char *dest, const char *src,
0165 const size_t destlen)
0166 {
0167 if (strlcpy(dest, src, destlen) > destlen)
0168 return -1;
0169 return 0;
0170 }
0171
0172 static int __init root_nfs_cat(char *dest, const char *src,
0173 const size_t destlen)
0174 {
0175 size_t len = strlen(dest);
0176
0177 if (len && dest[len - 1] != ',')
0178 if (strlcat(dest, ",", destlen) > destlen)
0179 return -1;
0180
0181 if (strlcat(dest, src, destlen) > destlen)
0182 return -1;
0183 return 0;
0184 }
0185
0186
0187
0188
0189
0190
0191
0192 static int __init root_nfs_parse_options(char *incoming, char *exppath,
0193 const size_t exppathlen)
0194 {
0195 char *p;
0196
0197
0198
0199
0200 p = strsep(&incoming, ",");
0201 if (*p != '\0' && strcmp(p, "default") != 0)
0202 if (root_nfs_copy(exppath, p, exppathlen))
0203 return -1;
0204
0205
0206
0207
0208
0209 if (incoming != NULL && *incoming != '\0')
0210 if (root_nfs_cat(nfs_root_options, incoming,
0211 sizeof(nfs_root_options)))
0212 return -1;
0213 return 0;
0214 }
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224 static int __init root_nfs_data(char *cmdline)
0225 {
0226 char mand_options[sizeof("nolock,addr=") + INET_ADDRSTRLEN + 1];
0227 int len, retval = -1;
0228 char *tmp = NULL;
0229 const size_t tmplen = sizeof(nfs_export_path);
0230
0231 tmp = kzalloc(tmplen, GFP_KERNEL);
0232 if (tmp == NULL)
0233 goto out_nomem;
0234 strcpy(tmp, NFS_ROOT);
0235
0236 if (root_server_path[0] != '\0') {
0237 dprintk("Root-NFS: DHCPv4 option 17: %s\n",
0238 root_server_path);
0239 if (root_nfs_parse_options(root_server_path, tmp, tmplen))
0240 goto out_optionstoolong;
0241 }
0242
0243 if (cmdline[0] != '\0') {
0244 dprintk("Root-NFS: nfsroot=%s\n", cmdline);
0245 if (root_nfs_parse_options(cmdline, tmp, tmplen))
0246 goto out_optionstoolong;
0247 }
0248
0249
0250
0251
0252
0253 snprintf(mand_options, sizeof(mand_options), "nolock,addr=%pI4",
0254 &servaddr);
0255 if (root_nfs_cat(nfs_root_options, mand_options,
0256 sizeof(nfs_root_options)))
0257 goto out_optionstoolong;
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269 len = snprintf(nfs_export_path, sizeof(nfs_export_path),
0270 tmp, utsname()->nodename);
0271 if (len >= (int)sizeof(nfs_export_path))
0272 goto out_devnametoolong;
0273 len = snprintf(nfs_root_device, sizeof(nfs_root_device),
0274 "%pI4:%s", &servaddr, nfs_export_path);
0275 if (len >= (int)sizeof(nfs_root_device))
0276 goto out_devnametoolong;
0277
0278 retval = 0;
0279
0280 out:
0281 kfree(tmp);
0282 return retval;
0283 out_nomem:
0284 printk(KERN_ERR "Root-NFS: could not allocate memory\n");
0285 goto out;
0286 out_optionstoolong:
0287 printk(KERN_ERR "Root-NFS: mount options string too long\n");
0288 goto out;
0289 out_devnametoolong:
0290 printk(KERN_ERR "Root-NFS: root device name too long.\n");
0291 goto out;
0292 }
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302 int __init nfs_root_data(char **root_device, char **root_data)
0303 {
0304 servaddr = root_server_addr;
0305 if (servaddr == htonl(INADDR_NONE)) {
0306 printk(KERN_ERR "Root-NFS: no NFS server address\n");
0307 return -1;
0308 }
0309
0310 if (root_nfs_data(nfs_root_parms) < 0)
0311 return -1;
0312
0313 *root_device = nfs_root_device;
0314 *root_data = nfs_root_options;
0315 return 0;
0316 }