zfs/tests/zfs-tests/cmd/cp_files.c

59 lines
1.1 KiB
C
Raw Normal View History

Fix ENOSPC in "Handle zap_add() failures in ..." Commit cc63068 caused ENOSPC error when copy a large amount of files between two directories. The reason is that the patch limits zap leaf expansion to 2 retries, and return ENOSPC when failed. The intent for limiting retries is to prevent pointlessly growing table to max size when adding a block full of entries with same name in different case in mixed mode. However, it turns out we cannot use any limit on the retry. When we copy files from one directory in readdir order, we are copying in hash order, one leaf block at a time. Which means that if the leaf block in source directory has expanded 6 times, and you copy those entries in that block, by the time you need to expand the leaf in destination directory, you need to expand it 6 times in one go. So any limit on the retry will result in error where it shouldn't. Note that while we do use different salt for different directories, it seems that the salt/hash function doesn't provide enough randomization to the hash distance to prevent this from happening. Since cc63068 has already been reverted. This patch adds it back and removes the retry limit. Also, as it turn out, failing on zap_add() has a serious side effect for mzap_upgrade(). When upgrading from micro zap to fat zap, it will call zap_add() to transfer entries one at a time. If it hit any error halfway through, the remaining entries will be lost, causing those files to become orphan. This patch add a VERIFY to catch it. Reviewed-by: Sanjeev Bagewadi <sanjeev.bagewadi@gmail.com> Reviewed-by: Richard Yao <ryao@gentoo.org> Reviewed-by: Tony Hutter <hutter2@llnl.gov> Reviewed-by: Albert Lee <trisk@forkgnu.org> Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Reviewed by: Matthew Ahrens <mahrens@delphix.com> Signed-off-by: Chunwei Chen <david.chen@nutanix.com> Closes #7401 Closes #7421
2018-04-18 21:19:50 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
int
main(int argc, char *argv[])
{
int tfd;
DIR *sdir;
struct dirent *dirent;
if (argc != 3) {
fprintf(stderr, "Usage: %s SRC DST\n", argv[0]);
exit(1);
}
sdir = opendir(argv[1]);
if (sdir == NULL) {
fprintf(stderr, "Failed to open %s: %s\n",
argv[1], strerror(errno));
exit(2);
}
tfd = open(argv[2], O_DIRECTORY);
if (tfd < 0) {
fprintf(stderr, "Failed to open %s: %s\n",
argv[2], strerror(errno));
closedir(sdir);
exit(3);
}
while ((dirent = readdir(sdir)) != NULL) {
if (dirent->d_name[0] == '.' &&
(dirent->d_name[1] == '.' || dirent->d_name[1] == '\0'))
continue;
int fd = openat(tfd, dirent->d_name, O_CREAT|O_WRONLY, 0666);
if (fd < 0) {
fprintf(stderr, "Failed to create %s/%s: %s\n",
argv[2], dirent->d_name, strerror(errno));
closedir(sdir);
close(tfd);
exit(4);
}
close(fd);
}
closedir(sdir);
close(tfd);
return (0);
}