Skip to content

Commit

Permalink
Merge pull request #1398 from trapexit/freebsd
Browse files Browse the repository at this point in the history
Improve FreeBSD compatibility
  • Loading branch information
trapexit authored Jan 16, 2025
2 parents dd906c8 + b83bd5f commit 978d067
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 27 deletions.
35 changes: 25 additions & 10 deletions libfuse/lib/cpu.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/*
ISC License
Copyright (c) 2024, Antonio SJ Musumeci <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "cpu.hpp"
#include "ghc/filesystem.hpp"
#include "fmt/core.h"

#include <pthread.h>
#include <sched.h>

#include <fstream>


int
CPU::getaffinity(const pthread_t thread_id_,
cpu_set_t *cpuset_)
CPU::getaffinity(cpu_set_t *cpuset_)
{
const pid_t pid = 0;
CPU_ZERO(cpuset_);
return sched_getaffinity(thread_id_,
return sched_getaffinity(pid,
sizeof(cpu_set_t),
cpuset_);
}
Expand Down Expand Up @@ -70,7 +85,7 @@ CPU::count()
int rv;
cpu_set_t cpuset;

rv = CPU::getaffinity(0,&cpuset);
rv = CPU::getaffinity(&cpuset);
if(rv < 0)
return rv;

Expand All @@ -83,7 +98,7 @@ CPU::cpus()
cpu_set_t cpuset;
CPU::CPUVec cpuvec;

CPU::getaffinity(0,&cpuset);
CPU::getaffinity(&cpuset);

for(int i = 0; i < CPU_SETSIZE; i++)
{
Expand All @@ -102,7 +117,7 @@ CPU::cpu2core()
cpu_set_t cpuset;
CPU::CPU2CoreMap c2c;

CPU::getaffinity(0,&cpuset);
CPU::getaffinity(&cpuset);

for(int i = 0; i < CPU_SETSIZE; i++)
{
Expand Down Expand Up @@ -135,7 +150,7 @@ CPU::core2cpus()
cpu_set_t cpuset;
CPU::Core2CPUsMap c2c;

CPU::getaffinity(0,&cpuset);
CPU::getaffinity(&cpuset);

for(int i = 0; i < CPU_SETSIZE; i++)
{
Expand Down
9 changes: 4 additions & 5 deletions libfuse/lib/cpu.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#pragma once

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <pthread.h>
#ifdef __FreeBSD__
#include <pthread_np.h>
#endif
#include <sched.h>

#include <set>
Expand All @@ -22,7 +21,7 @@ class CPU

public:
static int count();
static int getaffinity(const pthread_t thread_id, cpu_set_t *cpuset);
static int getaffinity(cpu_set_t *cpuset);
static int setaffinity(const pthread_t thread_id, cpu_set_t *cpuset);
static int setaffinity(const pthread_t thread_id, const int cpu);
static int setaffinity(const pthread_t thread_id, const std::set<int> cpus);
Expand Down
3 changes: 2 additions & 1 deletion mkdocs/docs/config/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ These options are the same regardless of whether you use them with the
mergerfs. (default: false)
- **lazy-umount-mountpoint=BOOL**: mergerfs will attempt to "lazy
umount" the mountpoint before mounting itself. Useful when
performing live upgrades of mergerfs. (default: false)
performing live upgrades of mergerfs. May not work on
FreeBSD. (default: false)
- **ignorepponrename=BOOL**: Ignore path preserving on
rename. Typically rename and link act differently depending on the
policy of `create` (read below). Enabling this will cause rename and
Expand Down
24 changes: 24 additions & 0 deletions mkdocs/docs/faq/technical_behavior_and_limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,27 @@ at best, provide equivalent performance, and in some cases, worse
performance. Splice is not supported on other platforms forcing a
traditional read/write fallback to be provided. The splice code was
removed to simplify the codebase.


## How does mergerfs handle credentials?


mergerfs is a multithreaded application in order to handle requests
from the kernel concurrently. Each FUSE message has a header with
certain details about the request include the process ID (pid) of the
requestion application, the process' effective user id (uid), and
group id (gid). To ensure proper POSIX filesystem behavior and
security mergerfs must change its identity to match that of the
requester when performing the core filesystem function on the
underlying filesystem. On most Unix/POSIX based system a process and
all its threads are under the same uid and gid. However, on Linux each
thread may have its own credentials. This allows mergerfs to be
multithreaded and for each thread to change to the credentials
(seteuid,setegid) as required by the incoming message it is
handling. However, on FreeBSD this is not possible at the moment
(though there has been
[discussions](https://wiki.freebsd.org/Per-Thread%20Credentials) and
as such must change the credentials of the whole application when
actioning messages. mergerfs does optimize this behavior by only
changing credentials and locking the thread to do so if the process is
currently not the same as what is necessary by the incoming request.
7 changes: 4 additions & 3 deletions mkdocs/docs/related_projects.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@

mergerfs can be found in the
[repositories](https://pkgs.org/download/mergerfs) of [many
Linux](https://repology.org/project/mergerfs/versions) (and maybe
FreeBSD) distributions.
Linux](https://repology.org/project/mergerfs/versions) distributions
and FreeBSD.

Note: Any non-rolling release based distro is likely to have
out-of-date versions.
Expand All @@ -51,6 +51,7 @@ out-of-date versions.
* [Gentoo](https://packages.gentoo.org/packages/sys-fs/mergerfs)
* [Arch (AUR)](https://aur.archlinux.org/packages/mergerfs)
* [Void](https://voidlinux.org/packages/?arch=x86_64&q=mergerfs)
* [NixOS](https://search.nixos.org/packages?channel=22.11&show=mergerfs&from=0&size=50&sort=relevance&type=packages&query=mergerfs)
* [NixOS](https://search.nixos.org/packages?type=packages&query=mergerfs)
* [Guix]()
* [Slackware](https://slackbuilds.org/repository/15.0/system/mergerfs/?search=mergerfs)
* [FreeBSD](https://www.freshports.org/filesystems/mergerfs)
15 changes: 13 additions & 2 deletions mkdocs/docs/setup/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ $ su -
# rpm -i rpmbuild/RPMS/<arch>/mergerfs-<version>.<arch>.rpm
```

## Generic
## Generic Linux

Have git, g++, make, python installed.
Have git, g++ or clang, make, python installed.


```
Expand All @@ -44,6 +44,17 @@ $ make
$ sudo make install
```

## FreeBSD

Have git, g++ or clang, gmake, python installed.

```
$ cd mergerfs
$ gmake
$ gmake install # as root
```


## Build options

```
Expand Down
29 changes: 26 additions & 3 deletions mkdocs/docs/setup/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,44 @@ wget https://github.com/trapexit/mergerfs/releases/download/<ver>/mergerfs-<ver>
sudo rpm -i mergerfs-<ver>.el<rel>.<arch>.rpm
```

## NixOS

[search.nixos.org](https://search.nixos.org/packages?channel=unstable&show=mergerfs&from=0&size=50&sort=relevance&type=packages&query=mergerfs)

```
nix-env -iA nixos.mergerfs
```


## ArchLinux

1. Setup AUR
2. `pacman -S mergerfs`


## Other
## Other Linux Distros

[Check your distro.](../related_projects.md#distributions-including-mergerfs)


## Static Linux Binaries

Static binaries are provided for situations where native packages are
unavailable.
If your distro does not package mergerfs there are static binaries
provided.

Get the tarball from the [releases page](https://github.com/trapexit/mergerfs/releases).

```
wget https://github.com/trapexit/mergerfs/releases/download/<ver>/mergerfs-static-linux_<arch>.tar.gz
sudo tar xvf mergerfs-static-linux_<arch>.tar.gz -C /
```


## FreeBSD

[https://www.freshports.org/filesystems/mergerfs](https://www.freshports.org/filesystems/mergerfs)


```
pkg install filesystems/mergerfs
```
6 changes: 5 additions & 1 deletion src/fs_readahead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
#include <fstream>
#include <string>

#include <sys/sysmacros.h>
#ifdef __FreeBSD__
# include <sys/types.h>
#else
# include <sys/sysmacros.h>
#endif

namespace l
{
Expand Down
10 changes: 9 additions & 1 deletion src/fs_umount2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@

#pragma once

#ifdef __FreeBSD__
# include <sys/param.h>
# include <sys/mount.h>
# define umount2(target,flags) unmount(target,flags)
# define MNT_DETACH 0
#else
# include <sys/mount.h>
#endif

#include <errno.h>
#include <sys/mount.h>

#include <string>

Expand Down
6 changes: 5 additions & 1 deletion tools/preload.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifdef __FreeBSD__
#define O_TMPFILE 0
#else
#define _GNU_SOURCE
#define _POSIX_C_SOURCE 200809L
//#define _POSIX_C_SOURCE 200809L
#endif

#include <assert.h>
#include <dlfcn.h>
Expand Down

0 comments on commit 978d067

Please sign in to comment.