162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 1995, 1996 Gero Kuhlmann <gero@gkminix.han.de> 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Allow an NFS filesystem to be mounted as root. The way this works is: 662306a36Sopenharmony_ci * (1) Use the IP autoconfig mechanism to set local IP addresses and routes. 762306a36Sopenharmony_ci * (2) Construct the device string and the options string using DHCP 862306a36Sopenharmony_ci * option 17 and/or kernel command line options. 962306a36Sopenharmony_ci * (3) When mount_root() sets up the root file system, pass these strings 1062306a36Sopenharmony_ci * to the NFS client's regular mount interface via sys_mount(). 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * 1362306a36Sopenharmony_ci * Changes: 1462306a36Sopenharmony_ci * 1562306a36Sopenharmony_ci * Alan Cox : Removed get_address name clash with FPU. 1662306a36Sopenharmony_ci * Alan Cox : Reformatted a bit. 1762306a36Sopenharmony_ci * Gero Kuhlmann : Code cleanup 1862306a36Sopenharmony_ci * Michael Rausch : Fixed recognition of an incoming RARP answer. 1962306a36Sopenharmony_ci * Martin Mares : (2.0) Auto-configuration via BOOTP supported. 2062306a36Sopenharmony_ci * Martin Mares : Manual selection of interface & BOOTP/RARP. 2162306a36Sopenharmony_ci * Martin Mares : Using network routes instead of host routes, 2262306a36Sopenharmony_ci * allowing the default configuration to be used 2362306a36Sopenharmony_ci * for normal operation of the host. 2462306a36Sopenharmony_ci * Martin Mares : Randomized timer with exponential backoff 2562306a36Sopenharmony_ci * installed to minimize network congestion. 2662306a36Sopenharmony_ci * Martin Mares : Code cleanup. 2762306a36Sopenharmony_ci * Martin Mares : (2.1) BOOTP and RARP made configuration options. 2862306a36Sopenharmony_ci * Martin Mares : Server hostname generation fixed. 2962306a36Sopenharmony_ci * Gerd Knorr : Fixed wired inode handling 3062306a36Sopenharmony_ci * Martin Mares : (2.2) "0.0.0.0" addresses from command line ignored. 3162306a36Sopenharmony_ci * Martin Mares : RARP replies not tested for server address. 3262306a36Sopenharmony_ci * Gero Kuhlmann : (2.3) Some bug fixes and code cleanup again (please 3362306a36Sopenharmony_ci * send me your new patches _before_ bothering 3462306a36Sopenharmony_ci * Linus so that I don' always have to cleanup 3562306a36Sopenharmony_ci * _afterwards_ - thanks) 3662306a36Sopenharmony_ci * Gero Kuhlmann : Last changes of Martin Mares undone. 3762306a36Sopenharmony_ci * Gero Kuhlmann : RARP replies are tested for specified server 3862306a36Sopenharmony_ci * again. However, it's now possible to have 3962306a36Sopenharmony_ci * different RARP and NFS servers. 4062306a36Sopenharmony_ci * Gero Kuhlmann : "0.0.0.0" addresses from command line are 4162306a36Sopenharmony_ci * now mapped to INADDR_NONE. 4262306a36Sopenharmony_ci * Gero Kuhlmann : Fixed a bug which prevented BOOTP path name 4362306a36Sopenharmony_ci * from being used (thanks to Leo Spiekman) 4462306a36Sopenharmony_ci * Andy Walker : Allow to specify the NFS server in nfs_root 4562306a36Sopenharmony_ci * without giving a path name 4662306a36Sopenharmony_ci * Swen Thümmler : Allow to specify the NFS options in nfs_root 4762306a36Sopenharmony_ci * without giving a path name. Fix BOOTP request 4862306a36Sopenharmony_ci * for domainname (domainname is NIS domain, not 4962306a36Sopenharmony_ci * DNS domain!). Skip dummy devices for BOOTP. 5062306a36Sopenharmony_ci * Jacek Zapala : Fixed a bug which prevented server-ip address 5162306a36Sopenharmony_ci * from nfsroot parameter from being used. 5262306a36Sopenharmony_ci * Olaf Kirch : Adapted to new NFS code. 5362306a36Sopenharmony_ci * Jakub Jelinek : Free used code segment. 5462306a36Sopenharmony_ci * Marko Kohtala : Fixed some bugs. 5562306a36Sopenharmony_ci * Martin Mares : Debug message cleanup 5662306a36Sopenharmony_ci * Martin Mares : Changed to use the new generic IP layer autoconfig 5762306a36Sopenharmony_ci * code. BOOTP and RARP moved there. 5862306a36Sopenharmony_ci * Martin Mares : Default path now contains host name instead of 5962306a36Sopenharmony_ci * host IP address (but host name defaults to IP 6062306a36Sopenharmony_ci * address anyway). 6162306a36Sopenharmony_ci * Martin Mares : Use root_server_addr appropriately during setup. 6262306a36Sopenharmony_ci * Martin Mares : Rewrote parameter parsing, now hopefully giving 6362306a36Sopenharmony_ci * correct overriding. 6462306a36Sopenharmony_ci * Trond Myklebust : Add in preliminary support for NFSv3 and TCP. 6562306a36Sopenharmony_ci * Fix bug in root_nfs_addr(). nfs_data.namlen 6662306a36Sopenharmony_ci * is NOT for the length of the hostname. 6762306a36Sopenharmony_ci * Hua Qin : Support for mounting root file system via 6862306a36Sopenharmony_ci * NFS over TCP. 6962306a36Sopenharmony_ci * Fabian Frederick: Option parser rebuilt (using parser lib) 7062306a36Sopenharmony_ci * Chuck Lever : Use super.c's text-based mount option parsing 7162306a36Sopenharmony_ci * Chuck Lever : Add "nfsrootdebug". 7262306a36Sopenharmony_ci */ 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci#include <linux/types.h> 7562306a36Sopenharmony_ci#include <linux/string.h> 7662306a36Sopenharmony_ci#include <linux/init.h> 7762306a36Sopenharmony_ci#include <linux/nfs.h> 7862306a36Sopenharmony_ci#include <linux/nfs_fs.h> 7962306a36Sopenharmony_ci#include <linux/utsname.h> 8062306a36Sopenharmony_ci#include <linux/root_dev.h> 8162306a36Sopenharmony_ci#include <net/ipconfig.h> 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci#include "internal.h" 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci#define NFSDBG_FACILITY NFSDBG_ROOT 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci/* Default path we try to mount. "%s" gets replaced by our IP address */ 8862306a36Sopenharmony_ci#define NFS_ROOT "/tftpboot/%s" 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci/* Default NFSROOT mount options. */ 9162306a36Sopenharmony_ci#if defined(CONFIG_NFS_V2) 9262306a36Sopenharmony_ci#define NFS_DEF_OPTIONS "vers=2,tcp,rsize=4096,wsize=4096" 9362306a36Sopenharmony_ci#elif defined(CONFIG_NFS_V3) 9462306a36Sopenharmony_ci#define NFS_DEF_OPTIONS "vers=3,tcp,rsize=4096,wsize=4096" 9562306a36Sopenharmony_ci#else 9662306a36Sopenharmony_ci#define NFS_DEF_OPTIONS "vers=4,tcp,rsize=4096,wsize=4096" 9762306a36Sopenharmony_ci#endif 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci/* Parameters passed from the kernel command line */ 10062306a36Sopenharmony_cistatic char nfs_root_parms[NFS_MAXPATHLEN + 1] __initdata = ""; 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci/* Text-based mount options passed to super.c */ 10362306a36Sopenharmony_cistatic char nfs_root_options[256] __initdata = NFS_DEF_OPTIONS; 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci/* Address of NFS server */ 10662306a36Sopenharmony_cistatic __be32 servaddr __initdata = htonl(INADDR_NONE); 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci/* Name of directory to mount */ 10962306a36Sopenharmony_cistatic char nfs_export_path[NFS_MAXPATHLEN + 1] __initdata = ""; 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci/* server:export path string passed to super.c */ 11262306a36Sopenharmony_cistatic char nfs_root_device[NFS_MAXPATHLEN + 1] __initdata = ""; 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci#ifdef NFS_DEBUG 11562306a36Sopenharmony_ci/* 11662306a36Sopenharmony_ci * When the "nfsrootdebug" kernel command line option is specified, 11762306a36Sopenharmony_ci * enable debugging messages for NFSROOT. 11862306a36Sopenharmony_ci */ 11962306a36Sopenharmony_cistatic int __init nfs_root_debug(char *__unused) 12062306a36Sopenharmony_ci{ 12162306a36Sopenharmony_ci nfs_debug |= NFSDBG_ROOT | NFSDBG_MOUNT; 12262306a36Sopenharmony_ci return 1; 12362306a36Sopenharmony_ci} 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci__setup("nfsrootdebug", nfs_root_debug); 12662306a36Sopenharmony_ci#endif 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci/* 12962306a36Sopenharmony_ci * Parse NFS server and directory information passed on the kernel 13062306a36Sopenharmony_ci * command line. 13162306a36Sopenharmony_ci * 13262306a36Sopenharmony_ci * nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>] 13362306a36Sopenharmony_ci * 13462306a36Sopenharmony_ci * If there is a "%s" token in the <root-dir> string, it is replaced 13562306a36Sopenharmony_ci * by the ASCII-representation of the client's IP address. 13662306a36Sopenharmony_ci */ 13762306a36Sopenharmony_cistatic int __init nfs_root_setup(char *line) 13862306a36Sopenharmony_ci{ 13962306a36Sopenharmony_ci ROOT_DEV = Root_NFS; 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci if (line[0] == '/' || line[0] == ',' || (line[0] >= '0' && line[0] <= '9')) { 14262306a36Sopenharmony_ci strscpy(nfs_root_parms, line, sizeof(nfs_root_parms)); 14362306a36Sopenharmony_ci } else { 14462306a36Sopenharmony_ci size_t n = strlen(line) + sizeof(NFS_ROOT) - 1; 14562306a36Sopenharmony_ci if (n >= sizeof(nfs_root_parms)) 14662306a36Sopenharmony_ci line[sizeof(nfs_root_parms) - sizeof(NFS_ROOT) - 2] = '\0'; 14762306a36Sopenharmony_ci sprintf(nfs_root_parms, NFS_ROOT, line); 14862306a36Sopenharmony_ci } 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci /* 15162306a36Sopenharmony_ci * Extract the IP address of the NFS server containing our 15262306a36Sopenharmony_ci * root file system, if one was specified. 15362306a36Sopenharmony_ci * 15462306a36Sopenharmony_ci * Note: root_nfs_parse_addr() removes the server-ip from 15562306a36Sopenharmony_ci * nfs_root_parms, if it exists. 15662306a36Sopenharmony_ci */ 15762306a36Sopenharmony_ci root_server_addr = root_nfs_parse_addr(nfs_root_parms); 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci return 1; 16062306a36Sopenharmony_ci} 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci__setup("nfsroot=", nfs_root_setup); 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_cistatic int __init root_nfs_copy(char *dest, const char *src, 16562306a36Sopenharmony_ci const size_t destlen) 16662306a36Sopenharmony_ci{ 16762306a36Sopenharmony_ci if (strscpy(dest, src, destlen) == -E2BIG) 16862306a36Sopenharmony_ci return -1; 16962306a36Sopenharmony_ci return 0; 17062306a36Sopenharmony_ci} 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_cistatic int __init root_nfs_cat(char *dest, const char *src, 17362306a36Sopenharmony_ci const size_t destlen) 17462306a36Sopenharmony_ci{ 17562306a36Sopenharmony_ci size_t len = strlen(dest); 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci if (len && dest[len - 1] != ',') 17862306a36Sopenharmony_ci if (strlcat(dest, ",", destlen) >= destlen) 17962306a36Sopenharmony_ci return -1; 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci if (strlcat(dest, src, destlen) >= destlen) 18262306a36Sopenharmony_ci return -1; 18362306a36Sopenharmony_ci return 0; 18462306a36Sopenharmony_ci} 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci/* 18762306a36Sopenharmony_ci * Parse out root export path and mount options from 18862306a36Sopenharmony_ci * passed-in string @incoming. 18962306a36Sopenharmony_ci * 19062306a36Sopenharmony_ci * Copy the export path into @exppath. 19162306a36Sopenharmony_ci */ 19262306a36Sopenharmony_cistatic int __init root_nfs_parse_options(char *incoming, char *exppath, 19362306a36Sopenharmony_ci const size_t exppathlen) 19462306a36Sopenharmony_ci{ 19562306a36Sopenharmony_ci char *p; 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci /* 19862306a36Sopenharmony_ci * Set the NFS remote path 19962306a36Sopenharmony_ci */ 20062306a36Sopenharmony_ci p = strsep(&incoming, ","); 20162306a36Sopenharmony_ci if (*p != '\0' && strcmp(p, "default") != 0) 20262306a36Sopenharmony_ci if (root_nfs_copy(exppath, p, exppathlen)) 20362306a36Sopenharmony_ci return -1; 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci /* 20662306a36Sopenharmony_ci * @incoming now points to the rest of the string; if it 20762306a36Sopenharmony_ci * contains something, append it to our root options buffer 20862306a36Sopenharmony_ci */ 20962306a36Sopenharmony_ci if (incoming != NULL && *incoming != '\0') 21062306a36Sopenharmony_ci if (root_nfs_cat(nfs_root_options, incoming, 21162306a36Sopenharmony_ci sizeof(nfs_root_options))) 21262306a36Sopenharmony_ci return -1; 21362306a36Sopenharmony_ci return 0; 21462306a36Sopenharmony_ci} 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ci/* 21762306a36Sopenharmony_ci * Decode the export directory path name and NFS options from 21862306a36Sopenharmony_ci * the kernel command line. This has to be done late in order to 21962306a36Sopenharmony_ci * use a dynamically acquired client IP address for the remote 22062306a36Sopenharmony_ci * root directory path. 22162306a36Sopenharmony_ci * 22262306a36Sopenharmony_ci * Returns zero if successful; otherwise -1 is returned. 22362306a36Sopenharmony_ci */ 22462306a36Sopenharmony_cistatic int __init root_nfs_data(char *cmdline) 22562306a36Sopenharmony_ci{ 22662306a36Sopenharmony_ci char mand_options[sizeof("nolock,addr=") + INET_ADDRSTRLEN + 1]; 22762306a36Sopenharmony_ci int len, retval = -1; 22862306a36Sopenharmony_ci char *tmp = NULL; 22962306a36Sopenharmony_ci const size_t tmplen = sizeof(nfs_export_path); 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci tmp = kzalloc(tmplen, GFP_KERNEL); 23262306a36Sopenharmony_ci if (tmp == NULL) 23362306a36Sopenharmony_ci goto out_nomem; 23462306a36Sopenharmony_ci strcpy(tmp, NFS_ROOT); 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci if (root_server_path[0] != '\0') { 23762306a36Sopenharmony_ci dprintk("Root-NFS: DHCPv4 option 17: %s\n", 23862306a36Sopenharmony_ci root_server_path); 23962306a36Sopenharmony_ci if (root_nfs_parse_options(root_server_path, tmp, tmplen)) 24062306a36Sopenharmony_ci goto out_optionstoolong; 24162306a36Sopenharmony_ci } 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_ci if (cmdline[0] != '\0') { 24462306a36Sopenharmony_ci dprintk("Root-NFS: nfsroot=%s\n", cmdline); 24562306a36Sopenharmony_ci if (root_nfs_parse_options(cmdline, tmp, tmplen)) 24662306a36Sopenharmony_ci goto out_optionstoolong; 24762306a36Sopenharmony_ci } 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci /* 25062306a36Sopenharmony_ci * Append mandatory options for nfsroot so they override 25162306a36Sopenharmony_ci * what has come before 25262306a36Sopenharmony_ci */ 25362306a36Sopenharmony_ci snprintf(mand_options, sizeof(mand_options), "nolock,addr=%pI4", 25462306a36Sopenharmony_ci &servaddr); 25562306a36Sopenharmony_ci if (root_nfs_cat(nfs_root_options, mand_options, 25662306a36Sopenharmony_ci sizeof(nfs_root_options))) 25762306a36Sopenharmony_ci goto out_optionstoolong; 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_ci /* 26062306a36Sopenharmony_ci * Set up nfs_root_device. For NFS mounts, this looks like 26162306a36Sopenharmony_ci * 26262306a36Sopenharmony_ci * server:/path 26362306a36Sopenharmony_ci * 26462306a36Sopenharmony_ci * At this point, utsname()->nodename contains our local 26562306a36Sopenharmony_ci * IP address or hostname, set by ipconfig. If "%s" exists 26662306a36Sopenharmony_ci * in tmp, substitute the nodename, then shovel the whole 26762306a36Sopenharmony_ci * mess into nfs_root_device. 26862306a36Sopenharmony_ci */ 26962306a36Sopenharmony_ci len = snprintf(nfs_export_path, sizeof(nfs_export_path), 27062306a36Sopenharmony_ci tmp, utsname()->nodename); 27162306a36Sopenharmony_ci if (len >= (int)sizeof(nfs_export_path)) 27262306a36Sopenharmony_ci goto out_devnametoolong; 27362306a36Sopenharmony_ci len = snprintf(nfs_root_device, sizeof(nfs_root_device), 27462306a36Sopenharmony_ci "%pI4:%s", &servaddr, nfs_export_path); 27562306a36Sopenharmony_ci if (len >= (int)sizeof(nfs_root_device)) 27662306a36Sopenharmony_ci goto out_devnametoolong; 27762306a36Sopenharmony_ci 27862306a36Sopenharmony_ci retval = 0; 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ciout: 28162306a36Sopenharmony_ci kfree(tmp); 28262306a36Sopenharmony_ci return retval; 28362306a36Sopenharmony_ciout_nomem: 28462306a36Sopenharmony_ci printk(KERN_ERR "Root-NFS: could not allocate memory\n"); 28562306a36Sopenharmony_ci goto out; 28662306a36Sopenharmony_ciout_optionstoolong: 28762306a36Sopenharmony_ci printk(KERN_ERR "Root-NFS: mount options string too long\n"); 28862306a36Sopenharmony_ci goto out; 28962306a36Sopenharmony_ciout_devnametoolong: 29062306a36Sopenharmony_ci printk(KERN_ERR "Root-NFS: root device name too long.\n"); 29162306a36Sopenharmony_ci goto out; 29262306a36Sopenharmony_ci} 29362306a36Sopenharmony_ci 29462306a36Sopenharmony_ci/** 29562306a36Sopenharmony_ci * nfs_root_data - Return prepared 'data' for NFSROOT mount 29662306a36Sopenharmony_ci * @root_device: OUT: address of string containing NFSROOT device 29762306a36Sopenharmony_ci * @root_data: OUT: address of string containing NFSROOT mount options 29862306a36Sopenharmony_ci * 29962306a36Sopenharmony_ci * Returns zero and sets @root_device and @root_data if successful, 30062306a36Sopenharmony_ci * otherwise -1 is returned. 30162306a36Sopenharmony_ci */ 30262306a36Sopenharmony_ciint __init nfs_root_data(char **root_device, char **root_data) 30362306a36Sopenharmony_ci{ 30462306a36Sopenharmony_ci servaddr = root_server_addr; 30562306a36Sopenharmony_ci if (servaddr == htonl(INADDR_NONE)) { 30662306a36Sopenharmony_ci printk(KERN_ERR "Root-NFS: no NFS server address\n"); 30762306a36Sopenharmony_ci return -1; 30862306a36Sopenharmony_ci } 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ci if (root_nfs_data(nfs_root_parms) < 0) 31162306a36Sopenharmony_ci return -1; 31262306a36Sopenharmony_ci 31362306a36Sopenharmony_ci *root_device = nfs_root_device; 31462306a36Sopenharmony_ci *root_data = nfs_root_options; 31562306a36Sopenharmony_ci return 0; 31662306a36Sopenharmony_ci} 317