Btrfs-progs: Fix set_label_unmounted() with label length validation
[platform/upstream/btrfs-progs.git] / btrfslabel.c
1 /*
2  * Copyright (C) 2008 Morey Roof.   All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #define _GNU_SOURCE
20
21 #ifndef __CHECKER__
22 #include <sys/ioctl.h>
23 #include <sys/mount.h>
24 #include "ioctl.h"
25 #endif /* __CHECKER__ */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <dirent.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <linux/fs.h>
35 #include <linux/limits.h>
36 #include <ctype.h>
37 #include "kerncompat.h"
38 #include "ctree.h"
39 #include "utils.h"
40 #include "version.h"
41 #include "disk-io.h"
42 #include "transaction.h"
43
44 #define MOUNTED                        1
45 #define UNMOUNTED                      2
46 #define GET_LABEL                      3
47 #define SET_LABEL                      4
48
49 static int set_label_unmounted(const char *dev, const char *label)
50 {
51         struct btrfs_trans_handle *trans;
52         struct btrfs_root *root;
53         int ret;
54
55         ret = check_mounted(dev);
56         if (ret < 0) {
57                fprintf(stderr, "FATAL: error checking %s mount status\n", dev);
58                return -1;
59         }
60         if (ret > 0) {
61                 fprintf(stderr, "ERROR: dev %s is mounted, use mount point\n",
62                         dev);
63                 return -1;
64         }
65
66         if (strlen(label) > BTRFS_LABEL_SIZE - 1) {
67                 fprintf(stderr, "ERROR: Label %s is too long (max %d)\n",
68                         label, BTRFS_LABEL_SIZE - 1);
69                 return -1;
70         }
71
72         /* Open the super_block at the default location
73          * and as read-write.
74          */
75         root = open_ctree(dev, 0, 1);
76         if (!root) /* errors are printed by open_ctree() */
77                 return -1;
78
79         trans = btrfs_start_transaction(root, 1);
80         snprintf(root->fs_info->super_copy.label, BTRFS_LABEL_SIZE, "%s",
81                  label);
82         btrfs_commit_transaction(trans, root);
83
84         /* Now we close it since we are done. */
85         close_ctree(root);
86         return 0;
87 }
88
89 static int set_label_mounted(const char *mount_path, const char *label)
90 {
91         int fd;
92
93         fd = open(mount_path, O_RDONLY | O_NOATIME);
94         if (fd < 0) {
95                 fprintf(stderr, "ERROR: unable access to '%s'\n", mount_path);
96                 return -1;
97         }
98
99         if (ioctl(fd, BTRFS_IOC_SET_FSLABEL, label) < 0) {
100                 fprintf(stderr, "ERROR: unable to set label %s\n",
101                         strerror(errno));
102                 close(fd);
103                 return -1;
104         }
105
106         return 0;
107 }
108
109 int get_label_unmounted(char *dev)
110 {
111        struct btrfs_root *root;
112
113        /* Open the super_block at the default location
114         * and as read-only.
115         */
116        root = open_ctree(dev, 0, 0);
117
118        if(!root)
119          return -1;
120
121        fprintf(stdout, "%s\n", root->fs_info->super_copy.label);
122
123        /* Now we close it since we are done. */
124        close_ctree(root);
125        return 0;
126 }
127
128 int get_label(char *btrfs_dev)
129 {
130
131         int ret;
132         ret = check_mounted(btrfs_dev);
133         if (ret < 0)
134         {
135                fprintf(stderr, "FATAL: error checking %s mount status\n", btrfs_dev);
136                return -1;
137         }
138
139         if(ret != 0)
140         {
141                fprintf(stderr, "FATAL: the filesystem has to be unmounted\n");
142                return -2;
143         }
144         ret = get_label_unmounted(btrfs_dev);
145         return ret;
146 }
147
148
149 int set_label(char *btrfs_dev, char *label)
150 {
151         return is_existing_blk_or_reg_file(btrfs_dev) ?
152                 set_label_unmounted(btrfs_dev, label) :
153                 set_label_mounted(btrfs_dev, label);
154 }