Lines Matching refs:fd

42 #include "fd-util.h"
56 /* WARNING: Be careful with file system ioctls! When we get an fd, we
77 int fd;
85 fd = open(parent, flags);
86 if (fd < 0)
89 return fd;
109 int btrfs_is_filesystem(int fd) {
112 assert(fd >= 0);
114 if (fstatfs(fd, &sfs) < 0)
120 int btrfs_is_subvol_fd(int fd) {
123 assert(fd >= 0);
127 if (fstat(fd, &st) < 0)
133 return btrfs_is_filesystem(fd);
137 _cleanup_close_ int fd = -1;
141 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
142 if (fd < 0)
145 return btrfs_is_subvol_fd(fd);
150 _cleanup_close_ int fd = -1;
160 fd = open_parent(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
161 if (fd < 0)
162 return fd;
166 if (ioctl(fd, BTRFS_IOC_SUBVOL_CREATE, &args) < 0)
190 int btrfs_subvol_set_read_only_fd(int fd, bool b) {
194 assert(fd >= 0);
196 if (fstat(fd, &st) < 0)
202 if (ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags) < 0)
213 if (ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &nflags) < 0)
220 _cleanup_close_ int fd = -1;
222 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
223 if (fd < 0)
226 return btrfs_subvol_set_read_only_fd(fd, b);
229 int btrfs_subvol_get_read_only_fd(int fd) {
233 assert(fd >= 0);
235 if (fstat(fd, &st) < 0)
241 if (ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags) < 0)
297 int btrfs_get_block_device_fd(int fd, dev_t *dev) {
302 assert(fd >= 0);
305 r = btrfs_is_filesystem(fd);
311 if (ioctl(fd, BTRFS_IOC_FS_INFO, &fsi) < 0)
324 if (ioctl(fd, BTRFS_IOC_DEV_INFO, &di) < 0) {
348 _cleanup_close_ int fd = -1;
353 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
354 if (fd < 0)
357 return btrfs_get_block_device_fd(fd, dev);
360 int btrfs_subvol_get_id_fd(int fd, uint64_t *ret) {
366 assert(fd >= 0);
369 r = btrfs_is_filesystem(fd);
375 if (ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args) < 0)
382 int btrfs_subvol_get_id(int fd, const char *subvol, uint64_t *ret) {
385 assert(fd >= 0);
388 subvol_fd = openat(fd, subvol, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
466 int btrfs_subvol_get_info_fd(int fd, uint64_t subvol_id, BtrfsSubvolInfo *ret) {
486 assert(fd >= 0);
490 r = btrfs_subvol_get_id_fd(fd, &subvol_id);
494 r = btrfs_is_filesystem(fd);
508 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0)
558 int btrfs_qgroup_get_quota_fd(int fd, uint64_t qgroupid, BtrfsQuotaInfo *ret) {
580 assert(fd >= 0);
584 r = btrfs_subvol_get_id_fd(fd, &qgroupid);
588 r = btrfs_is_filesystem(fd);
602 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0) {
673 _cleanup_close_ int fd = -1;
675 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
676 if (fd < 0)
679 return btrfs_qgroup_get_quota_fd(fd, qgroupid, ret);
682 int btrfs_subvol_find_subtree_qgroup(int fd, uint64_t subvol_id, uint64_t *ret) {
687 assert(fd >= 0);
700 r = btrfs_subvol_get_id_fd(fd, &subvol_id);
711 n = btrfs_qgroup_find_parents(fd, subvol_id, &qgroups);
744 int btrfs_subvol_get_subtree_quota_fd(int fd, uint64_t subvol_id, BtrfsQuotaInfo *ret) {
748 assert(fd >= 0);
758 r = btrfs_subvol_find_subtree_qgroup(fd, subvol_id, &qgroupid);
762 return btrfs_qgroup_get_quota_fd(fd, qgroupid, ret);
766 _cleanup_close_ int fd = -1;
768 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
769 if (fd < 0)
772 return btrfs_subvol_get_subtree_quota_fd(fd, subvol_id, ret);
775 int btrfs_defrag_fd(int fd) {
778 assert(fd >= 0);
780 if (fstat(fd, &st) < 0)
786 if (ioctl(fd, BTRFS_IOC_DEFRAG, NULL) < 0)
793 _cleanup_close_ int fd = -1;
795 fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
796 if (fd < 0)
799 return btrfs_defrag_fd(fd);
802 int btrfs_quota_enable_fd(int fd, bool b) {
808 assert(fd >= 0);
810 r = btrfs_is_filesystem(fd);
816 if (ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args) < 0)
823 _cleanup_close_ int fd = -1;
825 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
826 if (fd < 0)
829 return btrfs_quota_enable_fd(fd, b);
832 int btrfs_qgroup_set_limit_fd(int fd, uint64_t qgroupid, uint64_t referenced_max) {
841 assert(fd >= 0);
844 r = btrfs_subvol_get_id_fd(fd, &qgroupid);
848 r = btrfs_is_filesystem(fd);
858 if (ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args) < 0) {
861 (void) btrfs_quota_scan_wait(fd);
875 _cleanup_close_ int fd = -1;
877 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
878 if (fd < 0)
881 return btrfs_qgroup_set_limit_fd(fd, qgroupid, referenced_max);
884 int btrfs_subvol_set_subtree_quota_limit_fd(int fd, uint64_t subvol_id, uint64_t referenced_max) {
888 assert(fd >= 0);
890 r = btrfs_subvol_find_subtree_qgroup(fd, subvol_id, &qgroupid);
894 return btrfs_qgroup_set_limit_fd(fd, qgroupid, referenced_max);
898 _cleanup_close_ int fd = -1;
900 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
901 if (fd < 0)
904 return btrfs_subvol_set_subtree_quota_limit_fd(fd, subvol_id, referenced_max);
907 int btrfs_resize_loopback_fd(int fd, uint64_t new_size, bool grow_only) {
923 r = btrfs_get_block_device_fd(fd, &dev);
965 if (ioctl(fd, BTRFS_IOC_RESIZE, &args) < 0)
977 if (ioctl(fd, BTRFS_IOC_RESIZE, &args) < 0)
982 (void) fsync(fd);
989 _cleanup_close_ int fd = -1;
991 fd = open(p, O_RDONLY|O_NOCTTY|O_CLOEXEC);
992 if (fd < 0)
995 return btrfs_resize_loopback_fd(fd, new_size, grow_only);
1023 static int qgroup_create_or_destroy(int fd, bool b, uint64_t qgroupid) {
1032 r = btrfs_is_filesystem(fd);
1039 if (ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args) < 0) {
1046 (void) btrfs_quota_scan_wait(fd);
1059 int btrfs_qgroup_create(int fd, uint64_t qgroupid) {
1060 return qgroup_create_or_destroy(fd, true, qgroupid);
1063 int btrfs_qgroup_destroy(int fd, uint64_t qgroupid) {
1064 return qgroup_create_or_destroy(fd, false, qgroupid);
1067 int btrfs_qgroup_destroy_recursive(int fd, uint64_t qgroupid) {
1081 n = btrfs_qgroup_find_parents(fd, qgroupid, &qgroups);
1092 r = btrfs_qgroup_unassign(fd, qgroupid, qgroups[i]);
1102 (void) btrfs_qgroup_destroy_recursive(fd, qgroups[i]);
1105 return btrfs_qgroup_destroy(fd, qgroupid);
1108 int btrfs_quota_scan_start(int fd) {
1111 assert(fd >= 0);
1113 if (ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &args) < 0)
1119 int btrfs_quota_scan_wait(int fd) {
1120 assert(fd >= 0);
1122 if (ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_WAIT) < 0)
1128 int btrfs_quota_scan_ongoing(int fd) {
1131 assert(fd >= 0);
1133 if (ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_STATUS, &args) < 0)
1139 static int qgroup_assign_or_unassign(int fd, bool b, uint64_t child, uint64_t parent) {
1148 r = btrfs_is_filesystem(fd);
1155 r = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
1158 (void) btrfs_quota_scan_wait(fd);
1170 (void) btrfs_quota_scan_start(fd);
1175 int btrfs_qgroup_assign(int fd, uint64_t child, uint64_t parent) {
1176 return qgroup_assign_or_unassign(fd, true, child, parent);
1179 int btrfs_qgroup_unassign(int fd, uint64_t child, uint64_t parent) {
1180 return qgroup_assign_or_unassign(fd, false, child, parent);
1183 static int subvol_remove_children(int fd, const char *subvolume, uint64_t subvol_id, BtrfsRemoveFlags flags) {
1203 assert(fd >= 0);
1206 if (fstat(fd, &st) < 0)
1212 subvol_fd = openat(fd, subvolume, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
1225 if (ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &vol_args) >= 0) {
1226 (void) btrfs_qgroup_destroy_recursive(fd, subvol_id); /* for the leaf subvolumes, the qgroup id is identical to the subvol id */
1242 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0)
1270 if (ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args) < 0)
1309 if (ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &vol_args) < 0)
1312 (void) btrfs_qgroup_destroy_recursive(fd, subvol_id);
1317 _cleanup_close_ int fd = -1;
1327 fd = open_parent(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
1328 if (fd < 0)
1329 return fd;
1331 return subvol_remove_children(fd, subvolume, 0, flags);
1334 int btrfs_subvol_remove_fd(int fd, const char *subvolume, BtrfsRemoveFlags flags) {
1335 return subvol_remove_children(fd, subvolume, 0, flags);
1338 int btrfs_qgroup_copy_limits(int fd, uint64_t old_qgroupid, uint64_t new_qgroupid) {
1363 r = btrfs_is_filesystem(fd);
1374 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0) {
1416 if (ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &qargs) < 0) {
1418 (void) btrfs_quota_scan_wait(fd);
1438 static int copy_quota_hierarchy(int fd, uint64_t old_subvol_id, uint64_t new_subvol_id) {
1444 assert(fd >= 0);
1449 n_old_qgroups = btrfs_qgroup_find_parents(fd, old_subvol_id, &old_qgroups);
1453 r = btrfs_subvol_get_parent(fd, old_subvol_id, &old_parent_id);
1460 n_old_parent_qgroups = btrfs_qgroup_find_parents(fd, old_parent_id, &old_parent_qgroups);
1495 return btrfs_subvol_auto_qgroup_fd(fd, new_subvol_id, insert_intermediary_qgroup);
1498 static int copy_subtree_quota_limits(int fd, uint64_t old_subvol, uint64_t new_subvol) {
1504 r = btrfs_qgroup_copy_limits(fd, old_subvol, new_subvol);
1510 r = btrfs_subvol_find_subtree_qgroup(fd, old_subvol, &old_subtree_qgroup);
1516 r = btrfs_subvol_find_subtree_qgroup(fd, new_subvol, &new_subtree_qgroup);
1522 r = btrfs_qgroup_copy_limits(fd, old_subtree_qgroup, new_subtree_qgroup);
1546 .fd = old_fd,
1769 int btrfs_qgroup_find_parents(int fd, uint64_t qgroupid, uint64_t **ret) {
1791 assert(fd >= 0);
1795 r = btrfs_subvol_get_id_fd(fd, &qgroupid);
1799 r = btrfs_is_filesystem(fd);
1813 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0) {
1857 int btrfs_subvol_auto_qgroup_fd(int fd, uint64_t subvol_id, bool insert_intermediary_qgroup) {
1863 assert(fd >= 0);
1903 r = btrfs_is_subvol_fd(fd);
1909 r = btrfs_subvol_get_id_fd(fd, &subvol_id);
1914 n = btrfs_qgroup_find_parents(fd, subvol_id, &qgroups);
1922 r = btrfs_subvol_get_parent(fd, subvol_id, &parent_subvol);
1929 n = btrfs_qgroup_find_parents(fd, parent_subvol, &qgroups);
1961 r = btrfs_qgroup_create(fd, new_qgroupid);
1968 r = btrfs_qgroup_assign(fd, new_qgroupid, qgroups[i]);
1971 (void) btrfs_qgroup_destroy_recursive(fd, new_qgroupid);
1979 r = btrfs_qgroup_assign(fd, subvol_id, new_qgroupid);
1982 (void) btrfs_qgroup_destroy_recursive(fd, new_qgroupid);
1994 r = btrfs_qgroup_assign(fd, subvol_id, qgroups[i]);
2006 _cleanup_close_ int fd = -1;
2008 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
2009 if (fd < 0)
2012 return btrfs_subvol_auto_qgroup_fd(fd, subvol_id, create_intermediary_qgroup);
2015 int btrfs_subvol_get_parent(int fd, uint64_t subvol_id, uint64_t *ret) {
2034 assert(fd >= 0);
2038 r = btrfs_subvol_get_id_fd(fd, &subvol_id);
2042 r = btrfs_is_filesystem(fd);
2056 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0)