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 error("failed to open %s: %s", object
, strerror(-ret
));
55 ret
= ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
);
58 error("failed to get flags for %s: %s", object
,
64 if (flags
& BTRFS_SUBVOL_RDONLY
)
65 fprintf(stdout
, "ro=true\n");
67 fprintf(stdout
, "ro=false\n");
72 if (!strcmp(value
, "true")) {
73 flags
|= BTRFS_SUBVOL_RDONLY
;
74 } else if (!strcmp(value
, "false")) {
75 flags
= flags
& ~BTRFS_SUBVOL_RDONLY
;
78 error("invalid value for property: %s", value
);
82 ret
= ioctl(fd
, BTRFS_IOC_SUBVOL_SETFLAGS
, &flags
);
85 error("failed to set flags for %s: %s", object
,
96 static int prop_label(enum prop_object_type type
,
104 ret
= set_label((char *) object
, (char *) value
);
106 char label
[BTRFS_LABEL_SIZE
];
108 ret
= get_label((char *) object
, label
);
110 fprintf(stdout
, "label=%s\n", label
);
116 static int prop_compression(enum prop_object_type type
,
124 DIR *dirstream
= NULL
;
126 char *xattr_name
= NULL
;
127 int open_flags
= value
? O_RDWR
: O_RDONLY
;
129 fd
= open_file_or_dir3(object
, &dirstream
, open_flags
);
132 error("failed to open %s: %s", object
, strerror(-ret
));
136 xattr_name
= malloc(XATTR_BTRFS_PREFIX_LEN
+ strlen(name
) + 1);
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';
146 sret
= fsetxattr(fd
, xattr_name
, value
, strlen(value
), 0);
148 sret
= fgetxattr(fd
, xattr_name
, NULL
, 0);
152 error("failed to %s compression for %s: %s",
153 value
? "set" : "get", object
, strerror(-ret
));
166 sret
= fgetxattr(fd
, xattr_name
, buf
, len
);
169 error("failed to get compression for %s: %s",
170 object
, strerror(-ret
));
173 fprintf(stdout
, "compression=%.*s\n", (int)len
, buf
);
181 close_file_or_dir(fd
, dirstream
);
186 const struct prop_handler prop_handlers
[] = {
187 {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol
,
189 {"label", "Set/get label of device.", 0,
190 prop_object_dev
| prop_object_root
, prop_label
},
191 {"compression", "Set/get compression for a file or directory", 0,
192 prop_object_inode
, prop_compression
},
193 {NULL
, NULL
, 0, 0, NULL
}