@@ -168,8 +168,8 @@ libbtrfs_objects = send-stream.o send-utils.o kernel-lib/rbtree.o btrfs-list.o \
kernel-shared/free-space-tree.o repair.o kernel-shared/inode-item.o \
kernel-shared/file-item.o \
kernel-lib/raid56.o kernel-lib/tables.o \
- common/device-scan.o common/path-utils.o \
- common/utils.o libbtrfsutil/subvolume.o libbtrfsutil/stubs.o \
+ common/device-scan.o common/path-utils.o common/utils.o \
+ common/sysfs.o libbtrfsutil/subvolume.o libbtrfsutil/stubs.o \
crypto/hash.o crypto/xxhash.o $(CRYPTO_OBJECTS)
libbtrfs_headers = send-stream.h send-utils.h send.h kernel-lib/rbtree.h btrfs-list.h \
crypto/crc32c.h kernel-lib/list.h kerncompat.h \
new file mode 100644
@@ -0,0 +1,60 @@
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <unistd.h>
+#include <sys/select.h>
+#include <uuid/uuid.h>
+
+#include "common/utils.h"
+
+static u8 fsid[BTRFS_UUID_SIZE] = {0};
+
+int sysfs_open(int fd, const char *filename)
+{
+ char fsid_str[BTRFS_UUID_UNPARSED_SIZE];
+ int ret;
+ char sysfs_file[128];
+
+ if (fsid[0] == 0) {
+ ret = get_fsid_fd(fd, fsid);
+ if (ret < 0)
+ return ret;
+ }
+
+ uuid_unparse(fsid, fsid_str);
+ snprintf(sysfs_file, 128, "/sys/fs/btrfs/%s/%s", fsid_str, filename);
+ return open(sysfs_file, O_RDONLY);
+}
+
+int sysfs_get_str_fd(int fd, char *val, int size)
+{
+ lseek(fd, 0, SEEK_SET);
+ memset(val, '\0', size);
+ return read(fd, val, size);
+}
+
+int get_exclusive_operation(int mp_fd, char *val)
+{
+ char *s;
+ int fd;
+ int n;
+
+ fd = sysfs_open(mp_fd, "exclusive_operation");
+ if (fd < 0)
+ return fd;
+
+ n = sysfs_get_str_fd(fd, val, BTRFS_SYSFS_EXOP_SIZE);
+ close(fd);
+
+ if (n < 0)
+ return n;
+
+ s = strchr(val, '\n');
+ if (!s)
+ return n;
+
+ *s = '\0';
+ return strlen(val);
+}
@@ -153,4 +153,7 @@ void init_rand_seed(u64 seed);
char *btrfs_test_for_multiple_profiles(int fd);
int btrfs_warn_multiple_profiles(int fd);
+#define BTRFS_SYSFS_EXOP_SIZE 16
+int get_exclusive_operation(int fd, char *val);
+
#endif