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.
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.
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.
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/xattr.h>
29 #define XATTR_BTRFS_PREFIX "btrfs."
30 #define XATTR_BTRFS_PREFIX_LEN (sizeof(XATTR_BTRFS_PREFIX) - 1)
33 * Defined as synonyms in attr/xattr.h
36 #define ENOATTR ENODATA
39 static int prop_read_only(enum prop_object_type type
,
48 fd
= open(object
, O_RDONLY
);
51 fprintf(stderr
, "ERROR: open %s failed. %s\n",
52 object
, strerror(-ret
));
56 ret
= ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
);
59 fprintf(stderr
, "ERROR: failed to get flags for %s. %s\n",
60 object
, strerror(-ret
));
65 if (flags
& BTRFS_SUBVOL_RDONLY
)
66 fprintf(stdout
, "ro=true\n");
68 fprintf(stdout
, "ro=false\n");
73 if (!strcmp(value
, "true")) {
74 flags
|= BTRFS_SUBVOL_RDONLY
;
75 } else if (!strcmp(value
, "false")) {
76 flags
= flags
& ~BTRFS_SUBVOL_RDONLY
;
79 fprintf(stderr
, "ERROR: invalid value for property.\n");
83 ret
= ioctl(fd
, BTRFS_IOC_SUBVOL_SETFLAGS
, &flags
);
86 fprintf(stderr
, "ERROR: failed to set flags for %s. %s\n",
87 object
, strerror(-ret
));
97 static int prop_label(enum prop_object_type type
,
105 ret
= set_label((char *) object
, (char *) value
);
107 char label
[BTRFS_LABEL_SIZE
];
109 ret
= get_label((char *) object
, label
);
111 fprintf(stdout
, "label=%s\n", label
);
117 static int prop_compression(enum prop_object_type type
,
125 DIR *dirstream
= NULL
;
127 char *xattr_name
= NULL
;
128 int open_flags
= value
? O_RDWR
: O_RDONLY
;
130 fd
= open_file_or_dir3(object
, &dirstream
, open_flags
);
133 fprintf(stderr
, "ERROR: open %s failed. %s\n",
134 object
, strerror(-ret
));
138 xattr_name
= malloc(XATTR_BTRFS_PREFIX_LEN
+ strlen(name
) + 1);
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';
148 sret
= fsetxattr(fd
, xattr_name
, value
, strlen(value
), 0);
150 sret
= fgetxattr(fd
, xattr_name
, NULL
, 0);
155 "ERROR: failed to %s compression for %s. %s\n",
156 value
? "set" : "get", object
, strerror(-ret
));
169 sret
= fgetxattr(fd
, xattr_name
, buf
, len
);
173 "ERROR: failed to get compression for %s. %s\n",
174 object
, strerror(-ret
));
177 fprintf(stdout
, "compression=%.*s\n", (int)len
, buf
);
185 close_file_or_dir(fd
, dirstream
);
191 const struct prop_handler prop_handlers
[] = {
192 {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol
,
194 {"label", "Set/get label of device.", 0,
195 prop_object_dev
| prop_object_root
, prop_label
},
196 {"compression", "Set/get compression for a file or directory", 0,
197 prop_object_inode
, prop_compression
},