Btrfs-progs: add support for the compression property
[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
123         fd = open_file_or_dir(object, &dirstream);
124         if (fd == -1) {
125                 ret = -errno;
126                 fprintf(stderr, "ERROR: open %s failed. %s\n",
127                         object, strerror(-ret));
128                 goto out;
129         }
130
131         xattr_name = malloc(XATTR_BTRFS_PREFIX_LEN + strlen(name));
132         if (!xattr_name) {
133                 ret = -ENOMEM;
134                 goto out;
135         }
136         memcpy(xattr_name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN);
137         memcpy(xattr_name + XATTR_BTRFS_PREFIX_LEN, name, strlen(name));
138
139         if (value)
140                 sret = fsetxattr(fd, xattr_name, value, strlen(value), 0);
141         else
142                 sret = fgetxattr(fd, xattr_name, NULL, 0);
143         if (sret < 0) {
144                 ret = -errno;
145                 if (ret != -ENODATA)
146                         fprintf(stderr,
147                                 "ERROR: failed to %s compression for %s. %s\n",
148                                 value ? "set" : "get", object, strerror(-ret));
149                 goto out;
150         }
151         if (!value) {
152                 size_t len = sret;
153
154                 buf = malloc(len);
155                 if (!buf) {
156                         ret = -ENOMEM;
157                         goto out;
158                 }
159                 sret = fgetxattr(fd, xattr_name, buf, len);
160                 if (sret < 0) {
161                         ret = -errno;
162                         fprintf(stderr,
163                                 "ERROR: failed to get compression for %s. %s\n",
164                                 object, strerror(-ret));
165                         goto out;
166                 }
167                 fprintf(stdout, "compression=%.*s\n", (int)len, buf);
168         }
169
170         ret = 0;
171 out:
172         free(xattr_name);
173         free(buf);
174         if (fd >= 0)
175                 close_file_or_dir(fd, dirstream);
176
177         return ret;
178 }
179
180
181 const struct prop_handler prop_handlers[] = {
182         {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
183          prop_read_only},
184         {"label", "Set/get label of device.", 0,
185          prop_object_dev | prop_object_root, prop_label},
186         {"compression", "Set/get compression for a file or directory", 0,
187          prop_object_inode, prop_compression},
188         {0, 0, 0, 0, 0}
189 };