btrfs-progs: tests: Test if mkfs.btrfs --rootdir can handle ng symlink
[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
25 #include "btrfsutil_internal.h"
26
27 PUBLIC enum btrfs_util_error btrfs_util_sync(const char *path)
28 {
29         enum btrfs_util_error err;
30         int fd;
31
32         fd = open(path, O_RDONLY);
33         if (fd == -1)
34                 return BTRFS_UTIL_ERROR_OPEN_FAILED;
35
36         err = btrfs_util_sync_fd(fd);
37         SAVE_ERRNO_AND_CLOSE(fd);
38         return err;
39 }
40
41 PUBLIC enum btrfs_util_error btrfs_util_sync_fd(int fd)
42 {
43         int ret;
44
45         ret = ioctl(fd, BTRFS_IOC_SYNC, NULL);
46         if (ret == -1)
47                 return BTRFS_UTIL_ERROR_SYNC_FAILED;
48
49         return BTRFS_UTIL_OK;
50 }
51
52 PUBLIC enum btrfs_util_error btrfs_util_start_sync(const char *path,
53                                                    uint64_t *transid)
54 {
55         enum btrfs_util_error err;
56         int fd;
57
58         fd = open(path, O_RDONLY);
59         if (fd == -1)
60                 return BTRFS_UTIL_ERROR_OPEN_FAILED;
61
62         err = btrfs_util_start_sync_fd(fd, transid);
63         SAVE_ERRNO_AND_CLOSE(fd);
64         return err;
65 }
66
67 PUBLIC enum btrfs_util_error btrfs_util_start_sync_fd(int fd, uint64_t *transid)
68 {
69         int ret;
70
71         ret = ioctl(fd, BTRFS_IOC_START_SYNC, transid);
72         if (ret == -1)
73                 return BTRFS_UTIL_ERROR_START_SYNC_FAILED;
74
75         return BTRFS_UTIL_OK;
76 }
77
78 PUBLIC enum btrfs_util_error btrfs_util_wait_sync(const char *path,
79                                                   uint64_t transid)
80 {
81         enum btrfs_util_error err;
82         int fd;
83
84         fd = open(path, O_RDONLY);
85         if (fd == -1)
86                 return BTRFS_UTIL_ERROR_OPEN_FAILED;
87
88         err = btrfs_util_wait_sync_fd(fd, transid);
89         SAVE_ERRNO_AND_CLOSE(fd);
90         return err;
91 }
92
93 PUBLIC enum btrfs_util_error btrfs_util_wait_sync_fd(int fd, uint64_t transid)
94 {
95         int ret;
96
97         ret = ioctl(fd, BTRFS_IOC_WAIT_SYNC, &transid);
98         if (ret == -1)
99                 return BTRFS_UTIL_ERROR_WAIT_SYNC_FAILED;
100
101         return BTRFS_UTIL_OK;
102 }