2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2017 Rick Macklem
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/extattr.h>
40 #include <sys/mount.h>
41 #include <sys/socket.h>
43 #include <sys/types.h>
44 #include <sys/sysctl.h>
45 #include <arpa/inet.h>
46 #include <netinet/in.h>
47 #include <nfs/nfssvc.h>
49 #include <fs/nfs/nfsproto.h>
50 #include <fs/nfs/nfskpiport.h>
51 #include <fs/nfs/nfs.h>
52 #include <fs/nfs/nfsrvstate.h>
54 static void usage(void) __dead2;
56 static struct option longopts[] = {
57 { "migrate", required_argument, NULL, 'm' },
58 { "mirror", required_argument, NULL, 'r' },
63 * This program creates a copy of the file's (first argument) data on the
64 * new/recovering DS mirror. If the file is already on the new/recovering
65 * DS, it will simply exit(0).
68 main(int argc, char *argv[])
70 struct nfsd_pnfsd_args pnfsdarg;
71 struct pnfsdsfile dsfile[NFSDEV_MAXMIRRORS];
74 struct addrinfo hints, *res, *nres;
75 struct sockaddr_in sin;
76 struct sockaddr_in6 sin6;
77 ssize_t xattrsize, xattrsize2;
79 int ch, fnd, fndzero, i, migrateit, mirrorcnt, mirrorit, ret;
81 char host[MNAMELEN + NI_MAXHOST + 2], *cp;
84 errx(1, "Must be run as root/su");
86 mirrorit = migrateit = 0;
87 pnfsdarg.dspath = pnfsdarg.curdspath = NULL;
88 while ((ch = getopt_long(argc, argv, "m:r:", longopts, NULL)) != -1) {
91 /* Migrate the file from the second DS to the first. */
93 errx(1, "-r and -m are mutually exclusive");
95 pnfsdarg.curdspath = optarg;
98 /* Mirror the file on the specified DS. */
100 errx(1, "-r and -m are mutually exclusive");
102 pnfsdarg.dspath = optarg;
110 if (migrateit != 0) {
113 pnfsdarg.dspath = *argv++;
114 } else if (argc != 1)
117 /* Get the pNFS service's mirror level. */
118 mirlen = sizeof(mirrorlevel);
119 ret = sysctlbyname("vfs.nfs.pnfsmirror", &mirrorlevel, &mirlen,
122 errx(1, "Can't get vfs.nfs.pnfsmirror");
124 if (pnfsdarg.dspath != NULL && pnfsdarg.curdspath != NULL &&
125 strcmp(pnfsdarg.dspath, pnfsdarg.curdspath) == 0)
126 errx(1, "Can't migrate to same server");
129 * The host address and directory where the data storage file is
130 * located is in the extended attribute "pnfsd.dsfile".
132 xattrsize = extattr_get_file(*argv, EXTATTR_NAMESPACE_SYSTEM,
133 "pnfsd.dsfile", dsfile, sizeof(dsfile));
134 mirrorcnt = xattrsize / sizeof(struct pnfsdsfile);
135 xattrsize2 = mirrorcnt * sizeof(struct pnfsdsfile);
136 if (mirrorcnt < 1 || xattrsize != xattrsize2)
137 errx(1, "Can't get extattr pnfsd.dsfile for %s", *argv);
139 /* See if there is a 0.0.0.0 entry. */
141 for (i = 0; i < mirrorcnt; i++) {
142 if (dsfile[i].dsf_sin.sin_family == AF_INET &&
143 dsfile[i].dsf_sin.sin_addr.s_addr == 0)
147 /* If already mirrored for default case, just exit(0); */
148 if (mirrorit == 0 && migrateit == 0 && (mirrorlevel < 2 ||
149 (fndzero == 0 && mirrorcnt >= mirrorlevel) ||
150 (fndzero != 0 && mirrorcnt > mirrorlevel)))
153 /* For the "-r" case, there must be a 0.0.0.0 entry. */
154 if (mirrorit != 0 && (fndzero == 0 || mirrorlevel < 2 ||
155 mirrorcnt < 2 || mirrorcnt > mirrorlevel))
158 /* For pnfsdarg.dspath set, if it is already in list, just exit(0); */
159 if (pnfsdarg.dspath != NULL) {
160 /* Check the dspath to see that it's an NFS mount. */
161 if (stat(pnfsdarg.dspath, &sb) < 0)
162 errx(1, "Can't stat %s", pnfsdarg.dspath);
163 if (!S_ISDIR(sb.st_mode))
164 errx(1, "%s is not a directory", pnfsdarg.dspath);
165 if (statfs(pnfsdarg.dspath, &sf) < 0)
166 errx(1, "Can't fsstat %s", pnfsdarg.dspath);
167 if (strcmp(sf.f_fstypename, "nfs") != 0)
168 errx(1, "%s is not an NFS mount", pnfsdarg.dspath);
169 if (strcmp(sf.f_mntonname, pnfsdarg.dspath) != 0)
170 errx(1, "%s is not the mounted-on dir for the new DS",
174 * Check the IP address of the NFS server against the entrie(s)
175 * in the extended attribute.
177 strlcpy(host, sf.f_mntfromname, sizeof(host));
178 cp = strchr(host, ':');
180 errx(1, "No <host>: in mount %s", host);
182 memset(&hints, 0, sizeof(hints));
183 hints.ai_family = PF_UNSPEC;
184 hints.ai_socktype = SOCK_STREAM;
185 if (getaddrinfo(host, NULL, &hints, &res) != 0)
186 errx(1, "Can't get address for %s", host);
187 for (i = 0; i < mirrorcnt; i++) {
189 while (nres != NULL) {
190 if (dsfile[i].dsf_sin.sin_family ==
193 * If there is already an entry for this
194 * DS, just exit(0), since copying isn't
197 if (nres->ai_family == AF_INET &&
198 nres->ai_addrlen >= sizeof(sin)) {
199 memcpy(&sin, nres->ai_addr,
201 if (sin.sin_addr.s_addr ==
202 dsfile[i].dsf_sin.sin_addr.s_addr)
204 } else if (nres->ai_family ==
205 AF_INET6 && nres->ai_addrlen >=
207 memcpy(&sin6, nres->ai_addr,
209 if (IN6_ARE_ADDR_EQUAL(&sin6.sin6_addr,
210 &dsfile[i].dsf_sin6.sin6_addr))
214 nres = nres->ai_next;
220 /* For "-m", the pnfsdarg.curdspath must be in the list. */
221 if (pnfsdarg.curdspath != NULL) {
222 /* Check pnfsdarg.curdspath to see that it's an NFS mount. */
223 if (stat(pnfsdarg.curdspath, &sb) < 0)
224 errx(1, "Can't stat %s", pnfsdarg.curdspath);
225 if (!S_ISDIR(sb.st_mode))
226 errx(1, "%s is not a directory", pnfsdarg.curdspath);
227 if (statfs(pnfsdarg.curdspath, &sf) < 0)
228 errx(1, "Can't fsstat %s", pnfsdarg.curdspath);
229 if (strcmp(sf.f_fstypename, "nfs") != 0)
230 errx(1, "%s is not an NFS mount", pnfsdarg.curdspath);
231 if (strcmp(sf.f_mntonname, pnfsdarg.curdspath) != 0)
232 errx(1, "%s is not the mounted-on dir of the cur DS",
236 * Check the IP address of the NFS server against the entrie(s)
237 * in the extended attribute.
239 strlcpy(host, sf.f_mntfromname, sizeof(host));
240 cp = strchr(host, ':');
242 errx(1, "No <host>: in mount %s", host);
244 memset(&hints, 0, sizeof(hints));
245 hints.ai_family = PF_UNSPEC;
246 hints.ai_socktype = SOCK_STREAM;
247 if (getaddrinfo(host, NULL, &hints, &res) != 0)
248 errx(1, "Can't get address for %s", host);
250 for (i = 0; i < mirrorcnt && fnd == 0; i++) {
252 while (nres != NULL) {
253 if (dsfile[i].dsf_sin.sin_family ==
256 * Note if the entry is found.
258 if (nres->ai_family == AF_INET &&
259 nres->ai_addrlen >= sizeof(sin)) {
260 memcpy(&sin, nres->ai_addr,
262 if (sin.sin_addr.s_addr ==
263 dsfile[i].dsf_sin.sin_addr.s_addr) {
267 } else if (nres->ai_family ==
268 AF_INET6 && nres->ai_addrlen >=
270 memcpy(&sin6, nres->ai_addr,
272 if (IN6_ARE_ADDR_EQUAL(&sin6.sin6_addr,
273 &dsfile[i].dsf_sin6.sin6_addr)) {
279 nres = nres->ai_next;
284 * If not found just exit(0), since it is not on the
291 /* Do the copy via the nfssvc() syscall. */
292 pnfsdarg.op = PNFSDOP_COPYMR;
293 pnfsdarg.mdspath = *argv;
294 ret = nfssvc(NFSSVC_PNFSDS, &pnfsdarg);
295 if (ret < 0 && errno != EEXIST)
296 err(1, "Copymr failed for file %s", *argv);
304 fprintf(stderr, "pnfsdscopymr [-r recovered-DS-mounted-on-path] "
305 "[-m soure-DS-mounted-on-path destination-DS-mounted-on-path] "