btrfs-progs: mkfs: create root directory with 755 permissions
[btrfs-progs-unstable/devel.git] / btrfslabel.c
blobbf7380241f371d1767f1aba0c7c5f312eec74327
1 /*
2 * Copyright (C) 2008 Morey Roof. All rights reserved.
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.
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.
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.
19 #define _GNU_SOURCE
21 #ifndef __CHECKER__
22 #include <sys/ioctl.h>
23 #include <sys/mount.h>
24 #include "ioctl.h"
25 #endif /* __CHECKER__ */
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"
44 #define MOUNTED 1
45 #define UNMOUNTED 2
46 #define GET_LABEL 3
47 #define SET_LABEL 4
49 static void change_label_unmounted(char *dev, char *nLabel)
51 struct btrfs_root *root;
52 struct btrfs_trans_handle *trans;
54 /* Open the super_block at the default location
55 * and as read-write.
57 root = open_ctree(dev, 0, 1);
58 if (!root) /* errors are printed by open_ctree() */
59 return;
61 trans = btrfs_start_transaction(root, 1);
62 strncpy(root->fs_info->super_copy.label, nLabel, BTRFS_LABEL_SIZE);
63 root->fs_info->super_copy.label[BTRFS_LABEL_SIZE-1] = 0;
64 btrfs_commit_transaction(trans, root);
66 /* Now we close it since we are done. */
67 close_ctree(root);
70 static void get_label_unmounted(char *dev)
72 struct btrfs_root *root;
74 /* Open the super_block at the default location
75 * and as read-only.
77 root = open_ctree(dev, 0, 0);
79 fprintf(stdout, "%s\n", root->fs_info->super_copy.label);
81 /* Now we close it since we are done. */
82 close_ctree(root);
85 int get_label(char *btrfs_dev)
88 int ret;
89 ret = check_mounted(btrfs_dev);
90 if (ret < 0)
92 fprintf(stderr, "FATAL: error checking %s mount status\n", btrfs_dev);
93 return -1;
96 if(ret != 0)
98 fprintf(stderr, "FATAL: the filesystem has to be unmounted\n");
99 return -2;
101 get_label_unmounted(btrfs_dev);
102 return 0;
106 int set_label(char *btrfs_dev, char *nLabel)
109 int ret;
110 ret = check_mounted(btrfs_dev);
111 if (ret < 0)
113 fprintf(stderr, "FATAL: error checking %s mount status\n", btrfs_dev);
114 return -1;
117 if(ret != 0)
119 fprintf(stderr, "FATAL: the filesystem has to be unmounted\n");
120 return -2;
122 change_label_unmounted(btrfs_dev, nLabel);
123 return 0;