libbtrfsutil: add filesystem sync helpers
[platform/upstream/btrfs-progs.git] / libbtrfsutil / filesystem.c
1 /*
2  * Copyright (C) 2018 Facebook
3  *
4  * This file is part of libbtrfsutil.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <sys/ioctl.h>
24 #include <linux/btrfs.h>
25
26 #include "btrfsutil_internal.h"
27
28 PUBLIC enum btrfs_util_error btrfs_util_sync(const char *path)
29 {
30         enum btrfs_util_error err;
31         int fd;
32
33         fd = open(path, O_RDONLY);
34         if (fd == -1)
35                 return BTRFS_UTIL_ERROR_OPEN_FAILED;
36
37         err = btrfs_util_sync_fd(fd);
38         SAVE_ERRNO_AND_CLOSE(fd);
39         return err;
40 }
41
42 PUBLIC enum btrfs_util_error btrfs_util_sync_fd(int fd)
43 {
44         int ret;
45
46         ret = ioctl(fd, BTRFS_IOC_SYNC, NULL);
47         if (ret == -1)
48                 return BTRFS_UTIL_ERROR_SYNC_FAILED;
49
50         return BTRFS_UTIL_OK;
51 }
52
53 PUBLIC enum btrfs_util_error btrfs_util_start_sync(const char *path,
54                                                    uint64_t *transid)
55 {
56         enum btrfs_util_error err;
57         int fd;
58
59         fd = open(path, O_RDONLY);
60         if (fd == -1)
61                 return BTRFS_UTIL_ERROR_OPEN_FAILED;
62
63         err = btrfs_util_start_sync_fd(fd, transid);
64         SAVE_ERRNO_AND_CLOSE(fd);
65         return err;
66 }
67
68 PUBLIC enum btrfs_util_error btrfs_util_start_sync_fd(int fd, uint64_t *transid)
69 {
70         int ret;
71
72         ret = ioctl(fd, BTRFS_IOC_START_SYNC, transid);
73         if (ret == -1)
74                 return BTRFS_UTIL_ERROR_START_SYNC_FAILED;
75
76         return BTRFS_UTIL_OK;
77 }
78
79 PUBLIC enum btrfs_util_error btrfs_util_wait_sync(const char *path,
80                                                   uint64_t transid)
81 {
82         enum btrfs_util_error err;
83         int fd;
84
85         fd = open(path, O_RDONLY);
86         if (fd == -1)
87                 return BTRFS_UTIL_ERROR_OPEN_FAILED;
88
89         err = btrfs_util_wait_sync_fd(fd, transid);
90         SAVE_ERRNO_AND_CLOSE(fd);
91         return err;
92 }
93
94 PUBLIC enum btrfs_util_error btrfs_util_wait_sync_fd(int fd, uint64_t transid)
95 {
96         int ret;
97
98         ret = ioctl(fd, BTRFS_IOC_WAIT_SYNC, &transid);
99         if (ret == -1)
100                 return BTRFS_UTIL_ERROR_WAIT_SYNC_FAILED;
101
102         return BTRFS_UTIL_OK;
103 }