1 Upstream-Status: Inappropriate [Backport]
2 From d1dc6a9cff7e2fe4f335ca783a4b033457b3e184 Mon Sep 17 00:00:00 2001
3 From: Goffredo Baroncelli <kreijack@inwind.it>
4 Date: Sun, 5 Dec 2010 17:46:44 +0000
5 Subject: [PATCH 11/15] Add the "btrfs filesystem label" command
9 this patch adds the command "btrfs filesystem label" to change (or show) the
10 label of a filesystem.
11 This patch is a subset of the one written previously by Morey Roof. I
12 included the user space part only. So it is possible only to change/show a
13 label of a *single device* and *unounted* filesystem.
15 The reason of excluding the kernel space part, is to simplify the patch in
16 order to speed the check and then the merging of the patch itself. In fact I
17 have to point out that in the past there was almost three attempts to propose
18 this patch, without success neither complaints.
20 Chris, let me know how you want to proceed. I know that you are very busy,
21 and you prefer to work to stabilize btrfs instead adding new feature. But I
22 think that changing a label is a *essential* feature for a filesystem
23 managing tool. Think about a mount by LABEL.
27 $ btrfs filesystem label <device>
31 $ btrfs filesystem label <device> <newlabel>
33 Please guys, give a look to the source.
36 You can pull the source from the branch "label" of the repository
37 http://cassiopea.homelinux.net/git/btrfs-progs-unstable.git
42 Signed-off-by: Chris Mason <chris.mason@oracle.com>
46 btrfs_cmds.c | 16 +++++++
48 btrfslabel.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50 man/btrfs.8.in | 19 +++++++++
51 utils.c | 57 ++++++++++++++++++++++++++
53 9 files changed, 222 insertions(+), 6 deletions(-)
54 create mode 100644 btrfslabel.c
55 create mode 100644 btrfslabel.h
57 diff --git a/Makefile b/Makefile
58 index d65f6a2..4b95d2f 100644
61 @@ -4,7 +4,7 @@ CFLAGS = -g -Werror -Os
62 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
63 root-tree.o dir-item.o file-item.o inode-item.o \
64 inode-map.o crc32c.o rbtree.o extent-cache.o extent_io.o \
65 - volumes.o utils.o btrfs-list.o
66 + volumes.o utils.o btrfs-list.o btrfslabel.o
69 CHECKFLAGS=-D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ -Wbitwise \
70 diff --git a/btrfs.c b/btrfs.c
71 index 62140ef..4cd4210 100644
74 @@ -108,11 +108,6 @@ static struct Command commands[] = {
75 "device delete", "<device> [<device>...] <path>\n"
76 "Remove a device from a filesystem."
79 - { 2, "filesystem label", "<label> <path>\n"
80 - "Set the label of a filesystem"
86 diff --git a/btrfs_cmds.c b/btrfs_cmds.c
87 index 26d4fcc..6de73f4 100644
93 #include "btrfs_cmds.h"
94 +#include "btrfslabel.h"
97 #define BLKGETSIZE64 0
98 @@ -874,6 +875,21 @@ int do_set_default_subvol(int nargs, char **argv)
102 +int do_change_label(int nargs, char **argv)
104 + /* check the number of argument */
106 + fprintf(stderr, "ERROR: '%s' requires maximum 2 args\n",
109 + }else if (nargs == 2){
110 + return get_label(argv[1]);
111 + } else { /* nargs == 0 */
112 + return set_label(argv[1], argv[2]);
117 int do_df_filesystem(int nargs, char **argv)
119 struct btrfs_ioctl_space_args *sargs;
120 diff --git a/btrfs_cmds.h b/btrfs_cmds.h
121 index 7bde191..ab722d4 100644
124 @@ -32,3 +32,4 @@ int list_subvols(int fd);
125 int do_df_filesystem(int nargs, char **argv);
126 int find_updated_files(int fd, u64 root_id, u64 oldest_gen);
127 int do_find_newer(int argc, char **argv);
128 +int do_change_label(int argc, char **argv);
129 diff --git a/btrfslabel.c b/btrfslabel.c
131 index 0000000..c9f4684
136 + * Copyright (C) 2008 Morey Roof. All rights reserved.
138 + * This program is free software; you can redistribute it and/or
139 + * modify it under the terms of the GNU General Public
140 + * License v2 as published by the Free Software Foundation.
142 + * This program is distributed in the hope that it will be useful,
143 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
144 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
145 + * General Public License for more details.
147 + * You should have received a copy of the GNU General Public
148 + * License along with this program; if not, write to the
149 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
150 + * Boston, MA 021110-1307, USA.
156 +#include <sys/ioctl.h>
157 +#include <sys/mount.h>
159 +#endif /* __CHECKER__ */
163 +#include <sys/types.h>
164 +#include <sys/stat.h>
168 +#include <linux/fs.h>
169 +#include <linux/limits.h>
171 +#include "kerncompat.h"
174 +#include "version.h"
175 +#include "disk-io.h"
176 +#include "transaction.h"
183 +static void change_label_unmounted(char *dev, char *nLabel)
185 + struct btrfs_root *root;
186 + struct btrfs_trans_handle *trans;
188 + /* Open the super_block at the default location
189 + * and as read-write.
191 + root = open_ctree(dev, 0, 1);
193 + trans = btrfs_start_transaction(root, 1);
194 + strncpy(root->fs_info->super_copy.label, nLabel, BTRFS_LABEL_SIZE);
195 + btrfs_commit_transaction(trans, root);
197 + /* Now we close it since we are done. */
201 +static void get_label_unmounted(char *dev)
203 + struct btrfs_root *root;
205 + /* Open the super_block at the default location
206 + * and as read-only.
208 + root = open_ctree(dev, 0, 0);
210 + fprintf(stdout, "%s\n", root->fs_info->super_copy.label);
212 + /* Now we close it since we are done. */
216 +int get_label(char *btrfs_dev)
220 + ret = check_mounted(btrfs_dev);
223 + fprintf(stderr, "FATAL: error checking %s mount status\n", btrfs_dev);
229 + fprintf(stderr, "FATAL: the filesystem has to be unmounted\n");
232 + get_label_unmounted(btrfs_dev);
237 +int set_label(char *btrfs_dev, char *nLabel)
241 + ret = check_mounted(btrfs_dev);
244 + fprintf(stderr, "FATAL: error checking %s mount status\n", btrfs_dev);
250 + fprintf(stderr, "FATAL: the filesystem has to be unmounted\n");
253 + change_label_unmounted(btrfs_dev, nLabel);
256 diff --git a/btrfslabel.h b/btrfslabel.h
258 index 0000000..abf43ad
265 +int get_label(char *btrfs_dev);
266 +int set_label(char *btrfs_dev, char *nLabel);
267 \ No newline at end of file
268 diff --git a/man/btrfs.8.in b/man/btrfs.8.in
269 index b9b8913..6f92f91 100644
272 @@ -19,6 +19,8 @@ btrfs \- control a btrfs filesystem
274 \fBbtrfs\fP \fBfilesystem resize\fP\fI [+/\-]<size>[gkm]|max <filesystem>\fP
276 +\fBbtrfs\fP \fBfilesystem label\fP\fI <dev> [newlabel]\fP
278 \fBbtrfs\fP \fBfilesystem defrag\fP\fI [options] <file>|<dir> [<file>|<dir>...]\fP
280 \fBbtrfs\fP \fBsubvolume find-new\fP\fI <subvolume> <last_gen>\fP
281 @@ -164,6 +166,23 @@ can expand the partition before enlarging the filesystem and shrink the
282 partition after reducing the size of the filesystem.
285 +\fBbtrfs\fP \fBfilesystem label\fP\fI <dev> [newlabel]\fP
286 +Show or update the label of a filesystem. \fI<dev>\fR is used to identify the
288 +If a \fInewlabel\fR optional argument is passed, the label is changed. The
289 +following costraints exist for a label:
291 +- the maximum allowable lenght shall be less or equal than 256 chars
293 +- the label shall not contain the '/' or '\\' characters.
295 +NOTE: Currently there are the following limitations:
297 +- the filesystem has to be unmounted
299 +- the filesystem should not have more than one device.
302 \fBfilesystem show\fR [<uuid>|<label>]\fR
303 Show the btrfs filesystem with some additional info. If no UUID or label is
304 passed, \fBbtrfs\fR show info of all the btrfs filesystem.
305 diff --git a/utils.c b/utils.c
306 index ad980ae..13373c9 100644
309 @@ -812,6 +812,39 @@ out_mntloop_err:
313 +/* Gets the mount point of btrfs filesystem that is using the specified device.
314 + * Returns 0 is everything is good, <0 if we have an error.
315 + * TODO: Fix this fucntion and check_mounted to work with multiple drive BTRFS
318 +int get_mountpt(char *dev, char *mntpt, size_t size)
320 + struct mntent *mnt;
324 + f = setmntent("/proc/mounts", "r");
328 + while ((mnt = getmntent(f)) != NULL )
330 + if (strcmp(dev, mnt->mnt_fsname) == 0)
332 + strncpy(mntpt, mnt->mnt_dir, size);
339 + /* We didn't find an entry so lets report an error */
347 struct list_head list;
349 @@ -1002,3 +1035,27 @@ char *pretty_sizes(u64 size)
354 + * Checks to make sure that the label matches our requirements.
356 + 0 if everything is safe and usable
357 + -1 if the label is too long
358 + -2 if the label contains an invalid character
360 +int check_label(char *input)
363 + int len = strlen(input);
365 + if (len > BTRFS_LABEL_SIZE) {
369 + for (i = 0; i < len; i++) {
370 + if (input[i] == '/' || input[i] == '\\') {
377 diff --git a/utils.h b/utils.h
378 index a28d7f4..c3004ae 100644
381 @@ -40,4 +40,6 @@ int check_mounted(const char *devicename);
382 int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
384 char *pretty_sizes(u64 size);
385 +int check_label(char *input);
386 +int get_mountpt(char *dev, char *mntpt, size_t size);