]> Sergey Matveev's repositories - public-inbox.git/commitdiff
rewrite Linux nodatacow use in pure Perl w/o system
authorEric Wong <e@80x24.org>
Sun, 30 Jan 2022 21:49:08 +0000 (21:49 +0000)
committerEric Wong <e@80x24.org>
Mon, 31 Jan 2022 02:04:11 +0000 (02:04 +0000)
btrfs is Linux-only at the moment (and likely to remain that way
for practical purposes).  So rely on Linux ABI stability and use
the `syscall' and `ioctl' perlops rather than relying on Inline::C.
Inline::C (and gcc||clang) are monstrous dependencies which we
can't expect users to have.

This makes supporting new architectures more difficult, but new
architectures come along rarely and this reduces the burden for
the majority of Linux users on popular architectures (while
still avoiding the distribution of pre-built binaries).

Link: https://public-inbox.org/meta/YbCPWGaJEkV6eWfo@codewreck.org/
14 files changed:
MANIFEST
devel/syscall-list
lib/PublicInbox/IMAPTracker.pm
lib/PublicInbox/LeiMailSync.pm
lib/PublicInbox/MiscIdx.pm
lib/PublicInbox/Msgmap.pm
lib/PublicInbox/NDC_PP.pm [deleted file]
lib/PublicInbox/Over.pm
lib/PublicInbox/SearchIdx.pm
lib/PublicInbox/SharedKV.pm
lib/PublicInbox/Spawn.pm
lib/PublicInbox/Syscall.pm
lib/PublicInbox/Xapcmd.pm
t/nodatacow.t

index 1287182d83ceac95d97c2c090fcc4175be3dfe15..ca8402105517bf32894e68b3754d5604f52b9e01 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -289,7 +289,6 @@ lib/PublicInbox/MsgIter.pm
 lib/PublicInbox/MsgTime.pm
 lib/PublicInbox/Msgmap.pm
 lib/PublicInbox/MultiGit.pm
-lib/PublicInbox/NDC_PP.pm
 lib/PublicInbox/NNTP.pm
 lib/PublicInbox/NNTPD.pm
 lib/PublicInbox/NNTPdeflate.pm
index 3d55df1fc1d7d1f4cbb466ff91efa2cfabd5e66e..a6b1bfa7781f0fbd10cc3f2330e37a9f584e6feb 100755 (executable)
@@ -26,8 +26,10 @@ system($cc, '-o', $x, $f, @cflags) == 0 or die "cc failed \$?=$?";
 exec($x);
 __DATA__
 #define _GNU_SOURCE
-#include <unistd.h>
 #include <sys/syscall.h>
+#include <sys/ioctl.h>
+#include <linux/fs.h>
+#include <unistd.h>
 #include <stdio.h>
 
 #define D(x) printf("$" #x " = %ld;\n", (long)x)
@@ -46,6 +48,12 @@ int main(void)
        D(SYS_inotify_add_watch);
        D(SYS_inotify_rm_watch);
        D(SYS_prctl);
+       D(SYS_fstatfs);
+#ifdef FS_IOC_GETFLAGS
+       printf("FS_IOC_GETFLAGS=%#lx\nFS_IOC_SETFLAGS=%#lx\n",
+               (unsigned long)FS_IOC_GETFLAGS, (unsigned long)FS_IOC_SETFLAGS);
+#endif
+
 #ifdef SYS_renameat2
        D(SYS_renameat2);
 #endif
index 2fd66440df04ee540f5ce2f96e6e749d44a2e49e..4efa8a7e7e38b774ee09709b1de027d7c4cd8b79 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2018-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 package PublicInbox::IMAPTracker;
 use strict;
@@ -75,11 +75,11 @@ sub new {
        }
        if (!-f $dbname) {
                require File::Path;
-               require PublicInbox::Spawn;
+               require PublicInbox::Syscall;
                my ($dir) = ($dbname =~ m!(.*?/)[^/]+\z!);
                File::Path::mkpath($dir);
+               PublicInbox::Syscall::nodatacow_dir($dir);
                open my $fh, '+>>', $dbname or die "failed to open $dbname: $!";
-               PublicInbox::Spawn::nodatacow_fd(fileno($fh));
        }
        my $self = bless { lock_path => "$dbname.lock", url => $url }, $class;
        $self->lock_acquire;
index 124eb96948c5421c1f1bd66478de7ca6bc0021ad..182b0c222412d35ef740353ffb1fd5629d0738aa 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # for maintaining synchronization between lei/store <=> Maildir|MH|IMAP|JMAP
@@ -15,9 +15,9 @@ sub dbh_new {
        my $f = $self->{filename};
        my $creat = $rw && !-s $f;
        if ($creat) {
-               require PublicInbox::Spawn;
+               require PublicInbox::Syscall;
                open my $fh, '+>>', $f or Carp::croak "open($f): $!";
-               PublicInbox::Spawn::nodatacow_fd(fileno($fh));
+               PublicInbox::Syscall::nodatacow_fh($fh);
        }
        my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
                AutoCommit => 1,
index f5a374b2997a8c27ec9879f6f3c2695ebc2283b1..dc15442d9acb9ba1b7d29c51512c58454c5512d5 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # like PublicInbox::SearchIdx, but for searching for non-mail messages.
@@ -16,11 +16,11 @@ use v5.10.1;
 use PublicInbox::InboxWritable;
 use PublicInbox::Search; # for SWIG Xapian and Search::Xapian compat
 use PublicInbox::SearchIdx qw(index_text term_generator add_val);
-use PublicInbox::Spawn qw(nodatacow_dir);
 use Carp qw(croak);
 use File::Path ();
 use PublicInbox::MiscSearch;
 use PublicInbox::Config;
+use PublicInbox::Syscall;
 my $json;
 
 sub new {
@@ -28,7 +28,7 @@ sub new {
        PublicInbox::SearchIdx::load_xapian_writable();
        my $mi_dir = "$eidx->{xpfx}/misc";
        File::Path::mkpath($mi_dir);
-       nodatacow_dir($mi_dir);
+       PublicInbox::Syscall::nodatacow_dir($mi_dir);
        my $flags = $PublicInbox::SearchIdx::DB_CREATE_OR_OPEN;
        $flags |= $PublicInbox::SearchIdx::DB_NO_SYNC if $eidx->{-no_fsync};
        $json //= PublicInbox::Config::json();
index 699a8bf0d9cda0d12ae618948f824c8a0c17f651..1041cd177ed2dbb5589228354b29a1bf0b2eccd4 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # bidirectional Message-ID <-> Article Number mapping for the NNTP
@@ -13,7 +13,6 @@ use v5.10.1;
 use DBI;
 use DBD::SQLite;
 use PublicInbox::Over;
-use PublicInbox::Spawn;
 use Scalar::Util qw(blessed);
 
 sub new_file {
@@ -53,7 +52,8 @@ sub tmp_clone {
        require File::Temp;
        my $tmp = "mm_tmp-$$-XXXX";
        my ($fh, $fn) = File::Temp::tempfile($tmp, EXLOCK => 0, DIR => $dir);
-       PublicInbox::Spawn::nodatacow_fd(fileno($fh));
+       require PublicInbox::Syscall;
+       PublicInbox::Syscall::nodatacow_fh($fh);
        $self->{dbh}->sqlite_backup_to_file($fn);
        $tmp = ref($self)->new_file($fn, 2);
        $tmp->{dbh}->do('PRAGMA journal_mode = MEMORY');
diff --git a/lib/PublicInbox/NDC_PP.pm b/lib/PublicInbox/NDC_PP.pm
deleted file mode 100644 (file)
index 57abccb..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
-# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
-
-# Pure-perl class for Linux non-Inline::C users to disable COW for btrfs
-package PublicInbox::NDC_PP;
-use strict;
-use v5.10.1;
-
-sub nodatacow_dir ($) {
-       my ($path) = @_;
-       open my $mh, '<', '/proc/self/mounts' or return;
-       for (grep(/ btrfs /, <$mh>)) {
-               my (undef, $mnt_path, $type) = split(/ /);
-               next if $type ne 'btrfs'; # in case of false-positive from grep
-
-               # weird chars are escaped as octal
-               $mnt_path =~ s/\\(0[0-9]{2})/chr(oct($1))/egs;
-               $mnt_path .= '/' unless $mnt_path =~ m!/\z!;
-               if (index($path, $mnt_path) == 0) {
-                       # error goes to stderr, but non-fatal for us
-                       system('chattr', '+C', $path);
-                       last;
-               }
-       }
-}
-
-sub nodatacow_fd ($) {
-       my ($fd) = @_;
-       return if $^O ne 'linux';
-       defined(my $path = readlink("/proc/self/fd/$fd")) or return;
-       nodatacow_dir($path);
-}
-
-1;
index 30ad949dd027153f30835f928c962d00e5d9f82a..786f9d922282ea3358c7664462ceb8b591d05e4d 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2018-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # for XOVER, OVER in NNTP, and feeds/homepage/threads in PSGI
@@ -18,11 +18,10 @@ sub dbh_new {
        my $f = delete $self->{filename};
        if (!-s $f) { # SQLite defaults mode to 0644, we want 0666
                if ($rw) {
-                       require PublicInbox::Spawn;
+                       require PublicInbox::Syscall;
                        my ($dir) = ($f =~ m!(.+)/[^/]+\z!);
-                       PublicInbox::Spawn::nodatacow_dir($dir);
+                       PublicInbox::Syscall::nodatacow_dir($dir);
                        open my $fh, '+>>', $f or die "failed to open $f: $!";
-                       PublicInbox::Spawn::nodatacow_fd(fileno($fh));
                } else {
                        $self->{filename} = $f; # die on stat() below:
                }
index 4e5d7d4417142250474ae0b3b30e25ad03d0c756..95b14c3a55de4c7adfa6bf58d700a98534850324 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 # based on notmuch, but with no concept of folders, files
 #
@@ -20,7 +20,7 @@ use Carp qw(croak carp);
 use POSIX qw(strftime);
 use Time::Local qw(timegm);
 use PublicInbox::OverIdx;
-use PublicInbox::Spawn qw(spawn nodatacow_dir);
+use PublicInbox::Spawn qw(spawn);
 use PublicInbox::Git qw(git_unquote);
 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
 use PublicInbox::Address;
@@ -139,7 +139,8 @@ sub idx_acquire {
                if (!-d $dir && (!$is_shard ||
                                ($is_shard && need_xapian($self)))) {
                        File::Path::mkpath($dir);
-                       nodatacow_dir($dir);
+                       require PublicInbox::Syscall;
+                       PublicInbox::Syscall::nodatacow_dir($dir);
                        $self->{-set_has_threadid_once} = 1;
                }
        }
index 4297efedb05742a7cd222c993546768c75cbde8e..95a3cb147d2973fa3c91e84593936c487d9723c6 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # fork()-friendly key-value store.  Will be used for making
@@ -49,9 +49,9 @@ sub new {
        my $f = $self->{filename} = "$dir/$base.sqlite3";
        $self->{lock_path} = $opt->{lock_path} // "$dir/$base.flock";
        unless (-s $f) {
-               PublicInbox::Spawn::nodatacow_dir($dir); # for journal/shm/wal
+               require PublicInbox::Syscall;
+               PublicInbox::Syscall::nodatacow_dir($dir); # for journal/shm/wal
                open my $fh, '+>>', $f or die "failed to open $f: $!";
-               PublicInbox::Spawn::nodatacow_fd(fileno($fh));
        }
        $self;
 }
index e0a51c2167e829549d20683470aa55b02c3556b5..137b80878a35bced4a0735542908190d3dce1d92 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # This allows vfork to be used for spawning subprocesses if
@@ -21,7 +21,7 @@ use Symbol qw(gensym);
 use Fcntl qw(LOCK_EX SEEK_SET);
 use IO::Handle ();
 use PublicInbox::ProcessPipe;
-our @EXPORT_OK = qw(which spawn popen_rd run_die nodatacow_dir);
+our @EXPORT_OK = qw(which spawn popen_rd run_die);
 our @RLIMITS = qw(RLIMIT_CPU RLIMIT_CORE RLIMIT_DATA);
 
 BEGIN {
@@ -268,62 +268,12 @@ void recv_cmd4(PerlIO *s, SV *buf, STRLEN n)
 #endif /* defined(CMSG_SPACE) && defined(CMSG_LEN) */
 ALL_LIBC
 
-# btrfs on Linux is copy-on-write (COW) by default.  As of Linux 5.7,
-# this still leads to fragmentation for SQLite and Xapian files where
-# random I/O happens, so we disable COW just for SQLite files and Xapian
-# directories.  Disabling COW disables checksumming, so we only do this
-# for regeneratable files, and not canonical git storage (git doesn't
-# checksum refs, only data under $GIT_DIR/objects).
-       my $set_nodatacow = $^O eq 'linux' ? <<'SET_NODATACOW' : '';
-#include <sys/ioctl.h>
-#include <sys/vfs.h>
-#include <linux/magic.h>
-#include <linux/fs.h>
-#include <dirent.h>
-
-void nodatacow_fd(int fd)
-{
-       struct statfs buf;
-       int val = 0;
-
-       if (fstatfs(fd, &buf) < 0) {
-               fprintf(stderr, "fstatfs: %s\\n", strerror(errno));
-               return;
-       }
-
-       /* only btrfs is known to have this problem, so skip for non-btrfs */
-       if (buf.f_type != BTRFS_SUPER_MAGIC)
-               return;
-
-       if (ioctl(fd, FS_IOC_GETFLAGS, &val) < 0) {
-               fprintf(stderr, "FS_IOC_GET_FLAGS: %s\\n", strerror(errno));
-               return;
-       }
-       val |= FS_NOCOW_FL;
-       if (ioctl(fd, FS_IOC_SETFLAGS, &val) < 0)
-               fprintf(stderr, "FS_IOC_SET_FLAGS: %s\\n", strerror(errno));
-}
-
-void nodatacow_dir(const char *dir)
-{
-       DIR *dh = opendir(dir);
-       int fd;
-
-       if (!dh) croak("opendir(%s): %s", dir, strerror(errno));
-       fd = dirfd(dh);
-       if (fd >= 0)
-               nodatacow_fd(fd);
-       /* ENOTSUP probably won't happen under Linux... */
-       closedir(dh);
-}
-SET_NODATACOW
-
        my $inline_dir = $ENV{PERL_INLINE_DIRECTORY} //= (
                        $ENV{XDG_CACHE_HOME} //
                        ( ($ENV{HOME} // '/nonexistent').'/.cache' )
                ).'/public-inbox/inline-c';
        warn "$inline_dir exists, not writable\n" if -e $inline_dir && !-w _;
-       $set_nodatacow = $all_libc = undef unless -d _ && -w _;
+       $all_libc = undef unless -d _ && -w _;
        if (defined $all_libc) {
                my $f = "$inline_dir/.public-inbox.lock";
                open my $oldout, '>&', \*STDOUT or die "dup(1): $!";
@@ -337,17 +287,10 @@ SET_NODATACOW
                # CentOS 7.x ships Inline 0.53, 0.64+ has built-in locking
                flock($fh, LOCK_EX) or die "LOCK_EX($f): $!";
                eval <<'EOM';
-use Inline C => $all_libc.$set_nodatacow, BUILD_NOISY => 1;
+use Inline C => $all_libc, BUILD_NOISY => 1;
 EOM
                my $err = $@;
                my $ndc_err = '';
-               if ($err && $set_nodatacow) { # missing Linux kernel headers
-                       $ndc_err = "with set_nodatacow: <\n$err\n>\n";
-                       undef $set_nodatacow;
-                       eval <<'EOM';
-use Inline C => $all_libc, BUILD_NOISY => 1;
-EOM
-               };
                $err = $@;
                open(STDERR, '>&', $olderr) or warn "restore stderr: $!";
                open(STDOUT, '>&', $oldout) or warn "restore stdout: $!";
@@ -356,22 +299,13 @@ EOM
                        my @msg = <$fh>;
                        warn "Inline::C build failed:\n",
                                $ndc_err, $err, "\n", @msg;
-                       $set_nodatacow = $all_libc = undef;
-               } elsif ($ndc_err) {
-                       warn "Inline::C build succeeded w/o set_nodatacow\n",
-                               "error $ndc_err";
+                       $all_libc = undef;
                }
        }
        unless ($all_libc) {
                require PublicInbox::SpawnPP;
                *pi_fork_exec = \&PublicInbox::SpawnPP::pi_fork_exec
        }
-       unless ($set_nodatacow) {
-               require PublicInbox::NDC_PP;
-               no warnings 'once';
-               *nodatacow_fd = \&PublicInbox::NDC_PP::nodatacow_fd;
-               *nodatacow_dir = \&PublicInbox::NDC_PP::nodatacow_dir;
-       }
 } # /BEGIN
 
 sub which ($) {
index c00385b94db84b63facf7a8d57296ac76b3b1421..bcfae2cbcae423ef70171c90f3e52be3932c00ad 100644 (file)
@@ -5,7 +5,7 @@
 # This license differs from the rest of public-inbox
 #
 # This module is Copyright (c) 2005 Six Apart, Ltd.
-# Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 #
 # All rights reserved.
 #
@@ -68,6 +68,7 @@ our (
      $SYS_renameat2,
      );
 
+my $SYS_fstatfs; # don't need fstatfs64, just statfs.f_type
 my $SFD_CLOEXEC = 02000000; # Perl does not expose O_CLOEXEC
 our $no_deprecated = 0;
 
@@ -96,18 +97,21 @@ if ($^O eq "linux") {
         $SYS_epoll_wait   = 256;
         $SYS_signalfd4 = 327;
         $SYS_renameat2 //= 353;
+       $SYS_fstatfs = 100;
     } elsif ($machine eq "x86_64") {
         $SYS_epoll_create = 213;
         $SYS_epoll_ctl    = 233;
         $SYS_epoll_wait   = 232;
         $SYS_signalfd4 = 289;
        $SYS_renameat2 //= 316;
+       $SYS_fstatfs = 138;
     } elsif ($machine eq 'x32') {
         $SYS_epoll_create = 1073742037;
         $SYS_epoll_ctl = 1073742057;
         $SYS_epoll_wait = 1073742056;
         $SYS_signalfd4 = 1073742113;
        $SYS_renameat2 //= 0x40000000 + 316;
+       $SYS_fstatfs = 138;
     } elsif ($machine eq 'sparc64') {
        $SYS_epoll_create = 193;
        $SYS_epoll_ctl = 194;
@@ -116,6 +120,7 @@ if ($^O eq "linux") {
        $SYS_signalfd4 = 317;
        $SYS_renameat2 //= 345;
        $SFD_CLOEXEC = 020000000;
+       $SYS_fstatfs = 158;
     } elsif ($machine =~ m/^parisc/) {
         $SYS_epoll_create = 224;
         $SYS_epoll_ctl    = 225;
@@ -129,6 +134,7 @@ if ($^O eq "linux") {
         $u64_mod_8        = 1;
         $SYS_signalfd4 = 313;
        $SYS_renameat2 //= 357;
+       $SYS_fstatfs = 100;
     } elsif ($machine eq "ppc") {
         $SYS_epoll_create = 236;
         $SYS_epoll_ctl    = 237;
@@ -136,6 +142,7 @@ if ($^O eq "linux") {
         $u64_mod_8        = 1;
         $SYS_signalfd4 = 313;
        $SYS_renameat2 //= 357;
+       $SYS_fstatfs = 100;
     } elsif ($machine =~ m/^s390/) {
         $SYS_epoll_create = 249;
         $SYS_epoll_ctl    = 250;
@@ -143,6 +150,7 @@ if ($^O eq "linux") {
         $u64_mod_8        = 1;
         $SYS_signalfd4 = 322;
        $SYS_renameat2 //= 347;
+       $SYS_fstatfs = 100;
     } elsif ($machine eq "ia64") {
         $SYS_epoll_create = 1243;
         $SYS_epoll_ctl    = 1244;
@@ -165,6 +173,7 @@ if ($^O eq "linux") {
         $no_deprecated    = 1;
         $SYS_signalfd4 = 74;
        $SYS_renameat2 //= 276;
+       $SYS_fstatfs = 44;
     } elsif ($machine =~ m/arm(v\d+)?.*l/) {
         # ARM OABI
         $SYS_epoll_create = 250;
@@ -173,6 +182,7 @@ if ($^O eq "linux") {
         $u64_mod_8        = 1;
         $SYS_signalfd4 = 355;
        $SYS_renameat2 //= 382;
+       $SYS_fstatfs = 100;
     } elsif ($machine =~ m/^mips64/) {
         $SYS_epoll_create = 5207;
         $SYS_epoll_ctl    = 5208;
@@ -180,6 +190,7 @@ if ($^O eq "linux") {
         $u64_mod_8        = 1;
         $SYS_signalfd4 = 5283;
        $SYS_renameat2 //= 5311;
+       $SYS_fstatfs = 5135;
     } elsif ($machine =~ m/^mips/) {
         $SYS_epoll_create = 4248;
         $SYS_epoll_ctl    = 4249;
@@ -187,6 +198,7 @@ if ($^O eq "linux") {
         $u64_mod_8        = 1;
         $SYS_signalfd4 = 4324;
        $SYS_renameat2 //= 4351;
+       $SYS_fstatfs = 4100;
     } else {
         # as a last resort, try using the *.ph files which may not
         # exist or may be wrong
@@ -323,6 +335,38 @@ sub rename_noreplace ($$) {
        }
 }
 
+sub nodatacow_fh {
+       return if !defined($SYS_fstatfs);
+       my $buf = '';
+       vec($buf, 120 * 8 - 1, 1) = 0;
+       my ($fh) = @_;
+       syscall($SYS_fstatfs, fileno($fh), $buf) == 0 or
+               return warn("fstatfs: $!\n");
+       my $f_type = unpack('l!', $buf); # statfs.f_type is a signed word
+       return if $f_type != 0x9123683E; # BTRFS_SUPER_MAGIC
+
+       state ($FS_IOC_GETFLAGS, $FS_IOC_SETFLAGS);
+       unless (defined $FS_IOC_GETFLAGS) {
+               if (substr($Config{byteorder}, 0, 4) eq '1234') {
+                       $FS_IOC_GETFLAGS = 0x80086601;
+                       $FS_IOC_SETFLAGS = 0x40086602;
+               } else { # Big endian
+                       $FS_IOC_GETFLAGS = 0x40086601;
+                       $FS_IOC_SETFLAGS = 0x80086602;
+               }
+       }
+       ioctl($fh, $FS_IOC_GETFLAGS, $buf) //
+               return warn("FS_IOC_GET_FLAGS: $!\n");
+       my $attr = unpack('l!', $buf);
+       return if ($attr & 0x00800000); # FS_NOCOW_FL;
+       ioctl($fh, $FS_IOC_SETFLAGS, pack('l', $attr | 0x00800000)) //
+               warn("FS_IOC_SET_FLAGS: $!\n");
+}
+
+sub nodatacow_dir {
+       if (open my $fh, '<', $_[0]) { nodatacow_fh($fh) }
+}
+
 1;
 
 =head1 WARRANTY
index 44e0f8e58dca6ac1374ce01d953252a9f7a4bb8c..106856369c689007470b7ec364796e29079821f6 100644 (file)
@@ -1,8 +1,9 @@
-# Copyright (C) 2018-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 package PublicInbox::Xapcmd;
 use strict;
-use PublicInbox::Spawn qw(which popen_rd nodatacow_dir);
+use PublicInbox::Spawn qw(which popen_rd);
+use PublicInbox::Syscall;
 use PublicInbox::Admin qw(setup_signals);
 use PublicInbox::Over;
 use PublicInbox::SearchIdx;
@@ -211,7 +212,7 @@ sub prepare_run {
                my $v = PublicInbox::Search::SCHEMA_VERSION();
                my $wip = File::Temp->newdir("xapian$v-XXXX", DIR => $dir);
                $tmp->{$old} = $wip;
-               nodatacow_dir($wip->dirname);
+               PublicInbox::Syscall::nodatacow_dir($wip->dirname);
                push @queue, [ $old, $wip ];
        } elsif (defined $old) {
                opendir my $dh, $old or die "Failed to opendir $old: $!\n";
@@ -242,7 +243,7 @@ sub prepare_run {
                        same_fs_or_die($old, $wip->dirname);
                        my $cur = "$old/$dn";
                        push @queue, [ $src // $cur , $wip ];
-                       nodatacow_dir($wip->dirname);
+                       PublicInbox::Syscall::nodatacow_dir($wip->dirname);
                        $tmp->{$cur} = $wip;
                }
                # mark old shards to be unlinked
@@ -443,7 +444,7 @@ sub cpdb ($$) { # cb_spawn callback
                $ft = File::Temp->newdir("$new.compact-XXXX", DIR => $dir);
                setup_signals();
                $tmp = $ft->dirname;
-               nodatacow_dir($tmp);
+               PublicInbox::Syscall::nodatacow_dir($tmp);
        } else {
                $tmp = $new;
        }
index 19247c109b0931b8340f4f75bc4d189bd5a5577a..0940d908e935772183fda449be0ff7d6b152e75c 100644 (file)
@@ -1,48 +1,42 @@
 #!perl -w
-# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use strict; use v5.10.1; use PublicInbox::TestCommon;
 use File::Temp 0.19;
-use_ok 'PublicInbox::NDC_PP';
+use_ok 'PublicInbox::Syscall';
+
+# btrfs on Linux is copy-on-write (COW) by default.  As of Linux 5.7,
+# this still leads to fragmentation for SQLite and Xapian files where
+# random I/O happens, so we disable COW just for SQLite files and Xapian
+# directories.  Disabling COW disables checksumming, so we only do this
+# for regeneratable files, and not canonical git storage (git doesn't
+# checksum refs, only data under $GIT_DIR/objects).
 
 SKIP: {
        my $nr = 2;
        skip 'test is Linux-only', $nr if $^O ne 'linux';
        my $dir = $ENV{BTRFS_TESTDIR};
        skip 'BTRFS_TESTDIR not defined', $nr unless defined $dir;
-       require_cmd('chattr', 1) or skip 'chattr(1) not installed', $nr;
+
        my $lsattr = require_cmd('lsattr', 1) or
                skip 'lsattr(1) not installed', $nr;
+
        my $tmp = File::Temp->newdir('nodatacow-XXXX', DIR => $dir);
        my $dn = $tmp->dirname;
 
        my $name = "$dn/pp.f";
        open my $fh, '>', $name or BAIL_OUT "open($name): $!";
-       my $pp_sub = \&PublicInbox::NDC_PP::nodatacow_fd;
-       $pp_sub->(fileno($fh));
+       PublicInbox::Syscall::nodatacow_fh($fh);
        my $res = xqx([$lsattr, $name]);
+
+       BAIL_OUT "lsattr(1) fails in $dir" if $?;
        like($res, qr/C.*\Q$name\E/, "`C' attribute set on fd with pure Perl");
 
        $name = "$dn/pp.d";
        mkdir($name) or BAIL_OUT "mkdir($name) $!";
-       PublicInbox::NDC_PP::nodatacow_dir($name);
+       PublicInbox::Syscall::nodatacow_dir($name);
        $res = xqx([$lsattr, '-d', $name]);
        like($res, qr/C.*\Q$name\E/, "`C' attribute set on dir with pure Perl");
-
-       $name = "$dn/ic.f";
-       my $ic_sub = \&PublicInbox::Spawn::nodatacow_fd;
-       $pp_sub == $ic_sub and
-               skip 'Inline::C or Linux kernel headers missing', 2;
-       open $fh, '>', $name or BAIL_OUT "open($name): $!";
-       $ic_sub->(fileno($fh));
-       $res = xqx([$lsattr, $name]);
-       like($res, qr/C.*\Q$name\E/, "`C' attribute set on fd with Inline::C");
-
-       $name = "$dn/ic.d";
-       mkdir($name) or BAIL_OUT "mkdir($name) $!";
-       PublicInbox::Spawn::nodatacow_dir($name);
-       $res = xqx([$lsattr, '-d', $name]);
-       like($res, qr/C.*\Q$name\E/, "`C' attribute set on dir with Inline::C");
 };
 
 done_testing;