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 <attr/xattr.h>
29 #define XATTR_BTRFS_PREFIX "btrfs."
30 #define XATTR_BTRFS_PREFIX_LEN (sizeof(XATTR_BTRFS_PREFIX) - 1)
33 static int prop_read_only(enum prop_object_type type
,
42 fd
= open(object
, O_RDONLY
);
45 fprintf(stderr
, "ERROR: open %s failed. %s\n",
46 object
, strerror(-ret
));
50 ret
= ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
);
53 fprintf(stderr
, "ERROR: failed to get flags for %s. %s\n",
54 object
, strerror(-ret
));
59 if (flags
& BTRFS_SUBVOL_RDONLY
)
60 fprintf(stdout
, "ro=true\n");
62 fprintf(stdout
, "ro=false\n");
67 if (!strcmp(value
, "true")) {
68 flags
|= BTRFS_SUBVOL_RDONLY
;
69 } else if (!strcmp(value
, "false")) {
70 flags
= flags
& ~BTRFS_SUBVOL_RDONLY
;
73 fprintf(stderr
, "ERROR: invalid value for property.\n");
77 ret
= ioctl(fd
, BTRFS_IOC_SUBVOL_SETFLAGS
, &flags
);
80 fprintf(stderr
, "ERROR: failed to set flags for %s. %s\n",
81 object
, strerror(-ret
));
91 static int prop_label(enum prop_object_type type
,
99 ret
= set_label((char *) object
, (char *) value
);
101 char label
[BTRFS_LABEL_SIZE
];
103 ret
= get_label((char *) object
, label
);
105 fprintf(stdout
, "label=%s\n", label
);
111 static int prop_compression(enum prop_object_type type
,
119 DIR *dirstream
= NULL
;
121 char *xattr_name
= NULL
;
122 int open_flags
= value
? O_RDWR
: O_RDONLY
;
124 fd
= open_file_or_dir3(object
, &dirstream
, open_flags
);
127 fprintf(stderr
, "ERROR: open %s failed. %s\n",
128 object
, strerror(-ret
));
132 xattr_name
= malloc(XATTR_BTRFS_PREFIX_LEN
+ strlen(name
));
137 memcpy(xattr_name
, XATTR_BTRFS_PREFIX
, XATTR_BTRFS_PREFIX_LEN
);
138 memcpy(xattr_name
+ XATTR_BTRFS_PREFIX_LEN
, name
, strlen(name
));
141 sret
= fsetxattr(fd
, xattr_name
, value
, strlen(value
), 0);
143 sret
= fgetxattr(fd
, xattr_name
, NULL
, 0);
148 "ERROR: failed to %s compression for %s. %s\n",
149 value
? "set" : "get", object
, strerror(-ret
));
162 sret
= fgetxattr(fd
, xattr_name
, buf
, len
);
166 "ERROR: failed to get compression for %s. %s\n",
167 object
, strerror(-ret
));
170 fprintf(stdout
, "compression=%.*s\n", (int)len
, buf
);
178 close_file_or_dir(fd
, dirstream
);
184 const struct prop_handler prop_handlers
[] = {
185 {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol
,
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
},