Btrfs progs v4.17.1
[btrfs-progs-unstable/devel.git] / libbtrfsutil / filesystem.c
blob9c02b124e6198ebacd6c933ad58a84d8b7ec9d3e
1 /*
2 * Copyright (C) 2018 Facebook
4 * This file is part of libbtrfsutil.
6 * libbtrfsutil is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * libbtrfsutil is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with libbtrfsutil. If not, see <http://www.gnu.org/licenses/>.
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <sys/ioctl.h>
25 #include "btrfsutil_internal.h"
27 PUBLIC enum btrfs_util_error btrfs_util_sync(const char *path)
29 enum btrfs_util_error err;
30 int fd;
32 fd = open(path, O_RDONLY);
33 if (fd == -1)
34 return BTRFS_UTIL_ERROR_OPEN_FAILED;
36 err = btrfs_util_sync_fd(fd);
37 SAVE_ERRNO_AND_CLOSE(fd);
38 return err;
41 PUBLIC enum btrfs_util_error btrfs_util_sync_fd(int fd)
43 int ret;
45 ret = ioctl(fd, BTRFS_IOC_SYNC, NULL);
46 if (ret == -1)
47 return BTRFS_UTIL_ERROR_SYNC_FAILED;
49 return BTRFS_UTIL_OK;
52 PUBLIC enum btrfs_util_error btrfs_util_start_sync(const char *path,
53 uint64_t *transid)
55 enum btrfs_util_error err;
56 int fd;
58 fd = open(path, O_RDONLY);
59 if (fd == -1)
60 return BTRFS_UTIL_ERROR_OPEN_FAILED;
62 err = btrfs_util_start_sync_fd(fd, transid);
63 SAVE_ERRNO_AND_CLOSE(fd);
64 return err;
67 PUBLIC enum btrfs_util_error btrfs_util_start_sync_fd(int fd, uint64_t *transid)
69 int ret;
71 ret = ioctl(fd, BTRFS_IOC_START_SYNC, transid);
72 if (ret == -1)
73 return BTRFS_UTIL_ERROR_START_SYNC_FAILED;
75 return BTRFS_UTIL_OK;
78 PUBLIC enum btrfs_util_error btrfs_util_wait_sync(const char *path,
79 uint64_t transid)
81 enum btrfs_util_error err;
82 int fd;
84 fd = open(path, O_RDONLY);
85 if (fd == -1)
86 return BTRFS_UTIL_ERROR_OPEN_FAILED;
88 err = btrfs_util_wait_sync_fd(fd, transid);
89 SAVE_ERRNO_AND_CLOSE(fd);
90 return err;
93 PUBLIC enum btrfs_util_error btrfs_util_wait_sync_fd(int fd, uint64_t transid)
95 int ret;
97 ret = ioctl(fd, BTRFS_IOC_WAIT_SYNC, &transid);
98 if (ret == -1)
99 return BTRFS_UTIL_ERROR_WAIT_SYNC_FAILED;
101 return BTRFS_UTIL_OK;