btrfs-progs: Introduce change_header_uuid function
[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 <sys/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  * Defined as synonyms in attr/xattr.h
34  */
35 #ifndef ENOATTR
36 #define ENOATTR ENODATA
37 #endif
38
39 static int prop_read_only(enum prop_object_type type,
40                           const char *object,
41                           const char *name,
42                           const char *value)
43 {
44         int ret = 0;
45         int fd = -1;
46         u64 flags = 0;
47
48         fd = open(object, O_RDONLY);
49         if (fd < 0) {
50                 ret = -errno;
51                 fprintf(stderr, "ERROR: open %s failed. %s\n",
52                                 object, strerror(-ret));
53                 goto out;
54         }
55
56         ret = ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags);
57         if (ret < 0) {
58                 ret = -errno;
59                 fprintf(stderr, "ERROR: failed to get flags for %s. %s\n",
60                                 object, strerror(-ret));
61                 goto out;
62         }
63
64         if (!value) {
65                 if (flags & BTRFS_SUBVOL_RDONLY)
66                         fprintf(stdout, "ro=true\n");
67                 else
68                         fprintf(stdout, "ro=false\n");
69                 ret = 0;
70                 goto out;
71         }
72
73         if (!strcmp(value, "true")) {
74                 flags |= BTRFS_SUBVOL_RDONLY;
75         } else if (!strcmp(value, "false")) {
76                 flags = flags & ~BTRFS_SUBVOL_RDONLY;
77         } else {
78                 ret = -EINVAL;
79                 fprintf(stderr, "ERROR: invalid value for property.\n");
80                 goto out;
81         }
82
83         ret = ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &flags);
84         if (ret < 0) {
85                 ret = -errno;
86                 fprintf(stderr, "ERROR: failed to set flags for %s. %s\n",
87                                 object, strerror(-ret));
88                 goto out;
89         }
90
91 out:
92         if (fd != -1)
93                 close(fd);
94         return ret;
95 }
96
97 static int prop_label(enum prop_object_type type,
98                       const char *object,
99                       const char *name,
100                       const char *value)
101 {
102         int ret;
103
104         if (value) {
105                 ret = set_label((char *) object, (char *) value);
106         } else {
107                 char label[BTRFS_LABEL_SIZE];
108
109                 ret = get_label((char *) object, label);
110                 if (!ret)
111                         fprintf(stdout, "label=%s\n", label);
112         }
113
114         return ret;
115 }
116
117 static int prop_compression(enum prop_object_type type,
118                             const char *object,
119                             const char *name,
120                             const char *value)
121 {
122         int ret;
123         ssize_t sret;
124         int fd = -1;
125         DIR *dirstream = NULL;
126         char *buf = NULL;
127         char *xattr_name = NULL;
128         int open_flags = value ? O_RDWR : O_RDONLY;
129
130         fd = open_file_or_dir3(object, &dirstream, open_flags);
131         if (fd == -1) {
132                 ret = -errno;
133                 fprintf(stderr, "ERROR: open %s failed. %s\n",
134                         object, strerror(-ret));
135                 goto out;
136         }
137
138         xattr_name = malloc(XATTR_BTRFS_PREFIX_LEN + strlen(name) + 1);
139         if (!xattr_name) {
140                 ret = -ENOMEM;
141                 goto out;
142         }
143         memcpy(xattr_name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN);
144         memcpy(xattr_name + XATTR_BTRFS_PREFIX_LEN, name, strlen(name));
145         xattr_name[XATTR_BTRFS_PREFIX_LEN + strlen(name)] = '\0';
146
147         if (value)
148                 sret = fsetxattr(fd, xattr_name, value, strlen(value), 0);
149         else
150                 sret = fgetxattr(fd, xattr_name, NULL, 0);
151         if (sret < 0) {
152                 ret = -errno;
153                 if (ret != -ENOATTR)
154                         fprintf(stderr,
155                                 "ERROR: failed to %s compression for %s. %s\n",
156                                 value ? "set" : "get", object, strerror(-ret));
157                 else
158                         ret = 0;
159                 goto out;
160         }
161         if (!value) {
162                 size_t len = sret;
163
164                 buf = malloc(len);
165                 if (!buf) {
166                         ret = -ENOMEM;
167                         goto out;
168                 }
169                 sret = fgetxattr(fd, xattr_name, buf, len);
170                 if (sret < 0) {
171                         ret = -errno;
172                         fprintf(stderr,
173                                 "ERROR: failed to get compression for %s. %s\n",
174                                 object, strerror(-ret));
175                         goto out;
176                 }
177                 fprintf(stdout, "compression=%.*s\n", (int)len, buf);
178         }
179
180         ret = 0;
181 out:
182         free(xattr_name);
183         free(buf);
184         if (fd >= 0)
185                 close_file_or_dir(fd, dirstream);
186
187         return ret;
188 }
189
190 const struct prop_handler prop_handlers[] = {
191         {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
192          prop_read_only},
193         {"label", "Set/get label of device.", 0,
194          prop_object_dev | prop_object_root, prop_label},
195         {"compression", "Set/get compression for a file or directory", 0,
196          prop_object_inode, prop_compression},
197         {0, 0, 0, 0, 0}
198 };