btrfs-progs: dump-tree: print c/o/s/r time of ROOT_ITEM
[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                 error("failed to open %s: %s", object, strerror(-ret));
52                 goto out;
53         }
54
55         ret = ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags);
56         if (ret < 0) {
57                 ret = -errno;
58                 error("failed to get flags for %s: %s", object,
59                                 strerror(-ret));
60                 goto out;
61         }
62
63         if (!value) {
64                 if (flags & BTRFS_SUBVOL_RDONLY)
65                         fprintf(stdout, "ro=true\n");
66                 else
67                         fprintf(stdout, "ro=false\n");
68                 ret = 0;
69                 goto out;
70         }
71
72         if (!strcmp(value, "true")) {
73                 flags |= BTRFS_SUBVOL_RDONLY;
74         } else if (!strcmp(value, "false")) {
75                 flags = flags & ~BTRFS_SUBVOL_RDONLY;
76         } else {
77                 ret = -EINVAL;
78                 error("invalid value for property: %s", value);
79                 goto out;
80         }
81
82         ret = ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &flags);
83         if (ret < 0) {
84                 ret = -errno;
85                 error("failed to set flags for %s: %s", object,
86                                 strerror(-ret));
87                 goto out;
88         }
89
90 out:
91         if (fd != -1)
92                 close(fd);
93         return ret;
94 }
95
96 static int prop_label(enum prop_object_type type,
97                       const char *object,
98                       const char *name,
99                       const char *value)
100 {
101         int ret;
102
103         if (value) {
104                 ret = set_label((char *) object, (char *) value);
105         } else {
106                 char label[BTRFS_LABEL_SIZE];
107
108                 ret = get_label((char *) object, label);
109                 if (!ret)
110                         fprintf(stdout, "label=%s\n", label);
111         }
112
113         return ret;
114 }
115
116 static int prop_compression(enum prop_object_type type,
117                             const char *object,
118                             const char *name,
119                             const char *value)
120 {
121         int ret;
122         ssize_t sret;
123         int fd = -1;
124         DIR *dirstream = NULL;
125         char *buf = NULL;
126         char *xattr_name = NULL;
127         int open_flags = value ? O_RDWR : O_RDONLY;
128
129         fd = open_file_or_dir3(object, &dirstream, open_flags);
130         if (fd == -1) {
131                 ret = -errno;
132                 error("failed to open %s: %s", object, strerror(-ret));
133                 goto out;
134         }
135
136         xattr_name = malloc(XATTR_BTRFS_PREFIX_LEN + strlen(name) + 1);
137         if (!xattr_name) {
138                 ret = -ENOMEM;
139                 goto out;
140         }
141         memcpy(xattr_name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN);
142         memcpy(xattr_name + XATTR_BTRFS_PREFIX_LEN, name, strlen(name));
143         xattr_name[XATTR_BTRFS_PREFIX_LEN + strlen(name)] = '\0';
144
145         if (value) {
146                 if (strcmp(value, "no") == 0 || strcmp(value, "none") == 0)
147                         value = "";
148                 sret = fsetxattr(fd, xattr_name, value, strlen(value), 0);
149         } else {
150                 sret = fgetxattr(fd, xattr_name, NULL, 0);
151         }
152         if (sret < 0) {
153                 ret = -errno;
154                 if (ret != -ENOATTR)
155                         error("failed to %s compression for %s: %s",
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                         error("failed to get compression for %s: %s",
173                               object, strerror(-ret));
174                         goto out;
175                 }
176                 fprintf(stdout, "compression=%.*s\n", (int)len, buf);
177         }
178
179         ret = 0;
180 out:
181         free(xattr_name);
182         free(buf);
183         if (fd >= 0)
184                 close_file_or_dir(fd, dirstream);
185
186         return ret;
187 }
188
189 const struct prop_handler prop_handlers[] = {
190         {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
191          prop_read_only},
192         {"label", "Set/get label of device.", 0,
193          prop_object_dev | prop_object_root, prop_label},
194         {"compression", "Set/get compression for a file or directory", 0,
195          prop_object_inode, prop_compression},
196         {NULL, NULL, 0, 0, NULL}
197 };