Btrfs-progs: allow compression property gets for read-only subvolumes
[platform/upstream/btrfs-progs.git] / props.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public
4  * License v2 as published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9  * General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the
13  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14  * Boston, MA 021110-1307, USA.
15  */
16
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <attr/xattr.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23
24 #include "ctree.h"
25 #include "commands.h"
26 #include "utils.h"
27 #include "props.h"
28
29 #define XATTR_BTRFS_PREFIX     "btrfs."
30 #define XATTR_BTRFS_PREFIX_LEN (sizeof(XATTR_BTRFS_PREFIX) - 1)
31
32
33 static int prop_read_only(enum prop_object_type type,
34                           const char *object,
35                           const char *name,
36                           const char *value)
37 {
38         int ret = 0;
39         int fd = -1;
40         u64 flags = 0;
41
42         fd = open(object, O_RDONLY);
43         if (fd < 0) {
44                 ret = -errno;
45                 fprintf(stderr, "ERROR: open %s failed. %s\n",
46                                 object, strerror(-ret));
47                 goto out;
48         }
49
50         ret = ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags);
51         if (ret < 0) {
52                 ret = -errno;
53                 fprintf(stderr, "ERROR: failed to get flags for %s. %s\n",
54                                 object, strerror(-ret));
55                 goto out;
56         }
57
58         if (!value) {
59                 if (flags & BTRFS_SUBVOL_RDONLY)
60                         fprintf(stdout, "ro=true\n");
61                 else
62                         fprintf(stdout, "ro=false\n");
63                 ret = 0;
64                 goto out;
65         }
66
67         if (!strcmp(value, "true")) {
68                 flags |= BTRFS_SUBVOL_RDONLY;
69         } else if (!strcmp(value, "false")) {
70                 flags = flags & ~BTRFS_SUBVOL_RDONLY;
71         } else {
72                 ret = -EINVAL;
73                 fprintf(stderr, "ERROR: invalid value for property.\n");
74                 goto out;
75         }
76
77         ret = ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &flags);
78         if (ret < 0) {
79                 ret = -errno;
80                 fprintf(stderr, "ERROR: failed to set flags for %s. %s\n",
81                                 object, strerror(-ret));
82                 goto out;
83         }
84
85 out:
86         if (fd != -1)
87                 close(fd);
88         return ret;
89 }
90
91 static int prop_label(enum prop_object_type type,
92                       const char *object,
93                       const char *name,
94                       const char *value)
95 {
96         int ret;
97
98         if (value) {
99                 ret = set_label((char *) object, (char *) value);
100         } else {
101                 char label[BTRFS_LABEL_SIZE];
102
103                 ret = get_label((char *) object, label);
104                 if (!ret)
105                         fprintf(stdout, "label=%s\n", label);
106         }
107
108         return ret;
109 }
110
111 static int prop_compression(enum prop_object_type type,
112                             const char *object,
113                             const char *name,
114                             const char *value)
115 {
116         int ret;
117         ssize_t sret;
118         int fd = -1;
119         DIR *dirstream = NULL;
120         char *buf = NULL;
121         char *xattr_name = NULL;
122         int open_flags = value ? O_RDWR : O_RDONLY;
123
124         fd = open_file_or_dir3(object, &dirstream, open_flags);
125         if (fd == -1) {
126                 ret = -errno;
127                 fprintf(stderr, "ERROR: open %s failed. %s\n",
128                         object, strerror(-ret));
129                 goto out;
130         }
131
132         xattr_name = malloc(XATTR_BTRFS_PREFIX_LEN + strlen(name));
133         if (!xattr_name) {
134                 ret = -ENOMEM;
135                 goto out;
136         }
137         memcpy(xattr_name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN);
138         memcpy(xattr_name + XATTR_BTRFS_PREFIX_LEN, name, strlen(name));
139
140         if (value)
141                 sret = fsetxattr(fd, xattr_name, value, strlen(value), 0);
142         else
143                 sret = fgetxattr(fd, xattr_name, NULL, 0);
144         if (sret < 0) {
145                 ret = -errno;
146                 if (ret != -ENOATTR)
147                         fprintf(stderr,
148                                 "ERROR: failed to %s compression for %s. %s\n",
149                                 value ? "set" : "get", object, strerror(-ret));
150                 else
151                         ret = 0;
152                 goto out;
153         }
154         if (!value) {
155                 size_t len = sret;
156
157                 buf = malloc(len);
158                 if (!buf) {
159                         ret = -ENOMEM;
160                         goto out;
161                 }
162                 sret = fgetxattr(fd, xattr_name, buf, len);
163                 if (sret < 0) {
164                         ret = -errno;
165                         fprintf(stderr,
166                                 "ERROR: failed to get compression for %s. %s\n",
167                                 object, strerror(-ret));
168                         goto out;
169                 }
170                 fprintf(stdout, "compression=%.*s\n", (int)len, buf);
171         }
172
173         ret = 0;
174 out:
175         free(xattr_name);
176         free(buf);
177         if (fd >= 0)
178                 close_file_or_dir(fd, dirstream);
179
180         return ret;
181 }
182
183
184 const struct prop_handler prop_handlers[] = {
185         {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
186          prop_read_only},
187         {"label", "Set/get label of device.", 0,
188          prop_object_dev | prop_object_root, prop_label},
189         {"compression", "Set/get compression for a file or directory", 0,
190          prop_object_inode, prop_compression},
191         {0, 0, 0, 0, 0}
192 };