libbtrfsutil: add btrfs_util_is_subvolume() and btrfs_util_subvolume_id()
[platform/upstream/btrfs-progs.git] / libbtrfsutil / subvolume.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 <sys/stat.h>
25 #include <sys/types.h>
26 #include <sys/vfs.h>
27 #include <linux/magic.h>
28
29 #include "btrfsutil_internal.h"
30
31 /*
32  * This intentionally duplicates btrfs_util_is_subvolume_fd() instead of opening
33  * a file descriptor and calling it, because fstat() and fstatfs() don't accept
34  * file descriptors opened with O_PATH on old kernels (before v3.6 and before
35  * v3.12, respectively), but stat() and statfs() can be called on a path that
36  * the user doesn't have read or write permissions to.
37  */
38 PUBLIC enum btrfs_util_error btrfs_util_is_subvolume(const char *path)
39 {
40         struct statfs sfs;
41         struct stat st;
42         int ret;
43
44         ret = statfs(path, &sfs);
45         if (ret == -1)
46                 return BTRFS_UTIL_ERROR_STATFS_FAILED;
47
48         if (sfs.f_type != BTRFS_SUPER_MAGIC) {
49                 errno = EINVAL;
50                 return BTRFS_UTIL_ERROR_NOT_BTRFS;
51         }
52
53         ret = stat(path, &st);
54         if (ret == -1)
55                 return BTRFS_UTIL_ERROR_STAT_FAILED;
56
57         if (st.st_ino != BTRFS_FIRST_FREE_OBJECTID || !S_ISDIR(st.st_mode)) {
58                 errno = EINVAL;
59                 return BTRFS_UTIL_ERROR_NOT_SUBVOLUME;
60         }
61
62         return BTRFS_UTIL_OK;
63 }
64
65 PUBLIC enum btrfs_util_error btrfs_util_is_subvolume_fd(int fd)
66 {
67         struct statfs sfs;
68         struct stat st;
69         int ret;
70
71         ret = fstatfs(fd, &sfs);
72         if (ret == -1)
73                 return BTRFS_UTIL_ERROR_STATFS_FAILED;
74
75         if (sfs.f_type != BTRFS_SUPER_MAGIC) {
76                 errno = EINVAL;
77                 return BTRFS_UTIL_ERROR_NOT_BTRFS;
78         }
79
80         ret = fstat(fd, &st);
81         if (ret == -1)
82                 return BTRFS_UTIL_ERROR_STAT_FAILED;
83
84         if (st.st_ino != BTRFS_FIRST_FREE_OBJECTID || !S_ISDIR(st.st_mode)) {
85                 errno = EINVAL;
86                 return BTRFS_UTIL_ERROR_NOT_SUBVOLUME;
87         }
88
89         return BTRFS_UTIL_OK;
90 }
91
92 PUBLIC enum btrfs_util_error btrfs_util_subvolume_id(const char *path,
93                                                      uint64_t *id_ret)
94 {
95         enum btrfs_util_error err;
96         int fd;
97
98         fd = open(path, O_RDONLY);
99         if (fd == -1)
100                 return BTRFS_UTIL_ERROR_OPEN_FAILED;
101
102         err = btrfs_util_subvolume_id_fd(fd, id_ret);
103         SAVE_ERRNO_AND_CLOSE(fd);
104         return err;
105 }
106
107 PUBLIC enum btrfs_util_error btrfs_util_subvolume_id_fd(int fd,
108                                                         uint64_t *id_ret)
109 {
110         struct btrfs_ioctl_ino_lookup_args args = {
111                 .treeid = 0,
112                 .objectid = BTRFS_FIRST_FREE_OBJECTID,
113         };
114         int ret;
115
116         ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
117         if (ret == -1) {
118                 close(fd);
119                 return BTRFS_UTIL_ERROR_INO_LOOKUP_FAILED;
120         }
121
122         *id_ret = args.treeid;
123
124         return BTRFS_UTIL_OK;
125 }