~ubuntu-branches/ubuntu/natty/redhat-cluster/natty

« back to all changes in this revision

Viewing changes to gfs2/tool/df.c

  • Committer: Bazaar Package Importer
  • Author(s): Ante Karamatic
  • Date: 2009-09-15 07:15:43 UTC
  • mfrom: (1.1.16 upstream) (10.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090915071543-vr089k11l3kl9llu
Tags: 3.0.2-2ubuntu1
Remove unnecessary build dep on libvolume-id-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <stdio.h>
2
 
#include <stdlib.h>
3
 
#include <string.h>
4
 
#include <stdint.h>
5
 
#include <inttypes.h>
6
 
#include <sys/types.h>
7
 
#include <sys/stat.h>
8
 
#include <linux/types.h>
9
 
#include <fcntl.h>
10
 
#include <unistd.h>
11
 
#include <sys/ioctl.h>
12
 
#include <limits.h>
13
 
#include <errno.h>
14
 
 
15
 
#define __user
16
 
#include <linux/gfs2_ondisk.h>
17
 
 
18
 
#include "gfs2_tool.h"
19
 
#include "libgfs2.h"
20
 
 
21
 
/**
22
 
 * anthropomorphize - make a uint64_t number more human
23
 
 */
24
 
static const char *anthropomorphize(unsigned long long inhuman_value)
25
 
{
26
 
        const char *symbols = " KMGTPE";
27
 
        int i;
28
 
        unsigned long long val = inhuman_value;
29
 
        static char out_val[32];
30
 
 
31
 
        memset(out_val, 0, sizeof(out_val));
32
 
        for (i = 0; i < 6 && val > 1024; i++)
33
 
                val /= 1024;
34
 
        sprintf(out_val, "%llu%c", val, symbols[i]);
35
 
        return out_val;
36
 
}
37
 
 
38
 
/**
39
 
 * printit - parse out and print values according to the output type
40
 
 */
41
 
static void printit(unsigned long long block_size, const char *label,
42
 
                    unsigned long long total, unsigned long long used,
43
 
                    unsigned long long free, unsigned int percentage)
44
 
{
45
 
        switch (output_type) {
46
 
        case OUTPUT_BLOCKS:
47
 
                printf("  %-15s%-15llu%-15llu%-15llu%u%%\n",
48
 
                       label, total, used, free, percentage);
49
 
                break;
50
 
        case OUTPUT_K:
51
 
                printf("  %-15s%-15llu%-15llu%-15llu%u%%\n",
52
 
                       label, (total * block_size) / 1024,
53
 
                       (used * block_size) / 1024, (free * block_size) / 1024,
54
 
                       percentage);
55
 
                break;
56
 
        case OUTPUT_HUMAN:
57
 
                /* Need to do three separate printfs here because function
58
 
                   anthropomorphize re-uses the same static space. */
59
 
                printf("  %-15s%-15s", label,
60
 
                       anthropomorphize(total * block_size));
61
 
                printf("%-15s", anthropomorphize(used * block_size));
62
 
                printf("%-15s%u%%\n", anthropomorphize(free * block_size),
63
 
                       percentage);
64
 
                break;
65
 
        }
66
 
}
67
 
 
68
 
/**
69
 
 * do_df_one - print out information about one filesystem
70
 
 * @path: the path to the filesystem
71
 
 *
72
 
 */
73
 
 
74
 
static void
75
 
do_df_one(char *path)
76
 
{
77
 
        unsigned int percentage;
78
 
        unsigned int journals;
79
 
        uint64_t rgrps;
80
 
        unsigned int flags;
81
 
        char *value, *fs;
82
 
        int statfs_fd;
83
 
        struct gfs2_sbd sbd;
84
 
        char buf[GFS2_DEFAULT_BSIZE], statfs_fn[PATH_MAX];
85
 
        struct gfs2_statfs_change sc;
86
 
 
87
 
        memset(&sbd, 0, sizeof(struct gfs2_sbd));
88
 
        sbd.path_name = path;
89
 
        check_for_gfs2(&sbd);
90
 
        fs = mp2fsname(sbd.path_name);
91
 
 
92
 
        sbd.device_fd = open(sbd.device_name, O_RDONLY);
93
 
        if (sbd.device_fd < 0)
94
 
                die("can't open %s: %s\n", path, strerror(errno));
95
 
 
96
 
        sbd.bsize = GFS2_DEFAULT_BSIZE;
97
 
        sbd.jsize = GFS2_DEFAULT_JSIZE;
98
 
        sbd.rgsize = GFS2_DEFAULT_RGSIZE;
99
 
        sbd.utsize = GFS2_DEFAULT_UTSIZE;
100
 
        sbd.qcsize = GFS2_DEFAULT_QCSIZE;
101
 
        osi_list_init(&sbd.rglist);
102
 
        init_buf_list(&sbd, &sbd.buf_list, 128 << 20);
103
 
        init_buf_list(&sbd, &sbd.nvbuf_list, 0xffffffff);
104
 
 
105
 
        do_lseek(sbd.device_fd, 0x10 * sbd.bsize);
106
 
        do_read(sbd.device_fd, buf, sbd.bsize); /* read in the superblock */
107
 
 
108
 
        compute_constants(&sbd);
109
 
        gfs2_sb_in(&sbd.sd_sb, buf); /* parse it out into the sb structure */
110
 
 
111
 
        sbd.master_dir = gfs2_load_inode(&sbd,
112
 
                                         sbd.sd_sb.sb_master_dir.no_addr);
113
 
 
114
 
        gfs2_lookupi(sbd.master_dir, "rindex", 6, &sbd.md.riinode);
115
 
        gfs2_lookupi(sbd.master_dir, "jindex", 6, &sbd.md.jiinode);
116
 
        close(sbd.device_fd);
117
 
 
118
 
        journals = sbd.md.jiinode->i_di.di_entries - 2;
119
 
 
120
 
        rgrps = sbd.md.riinode->i_di.di_size;
121
 
        if (rgrps % sizeof(struct gfs2_rindex))
122
 
                die("bad rindex size\n");
123
 
        rgrps /= sizeof(struct gfs2_rindex);
124
 
 
125
 
        printf("%s:\n", path);
126
 
        printf("  SB lock proto = \"%s\"\n", sbd.sd_sb.sb_lockproto);
127
 
        printf("  SB lock table = \"%s\"\n", sbd.sd_sb.sb_locktable);
128
 
        printf("  SB ondisk format = %u\n", sbd.sd_sb.sb_fs_format);
129
 
        printf("  SB multihost format = %u\n", sbd.sd_sb.sb_multihost_format);
130
 
        printf("  Block size = %u\n", sbd.sd_sb.sb_bsize);
131
 
        printf("  Journals = %u\n", journals);
132
 
        printf("  Resource Groups = %"PRIu64"\n", rgrps);
133
 
        printf("  Mounted lock proto = \"%s\"\n",
134
 
               ((value = get_sysfs(fs, "args/lockproto"))[0])
135
 
               ? value : sbd.sd_sb.sb_lockproto);
136
 
        printf("  Mounted lock table = \"%s\"\n",
137
 
               ((value = get_sysfs(fs, "args/locktable"))[0])
138
 
               ? value : sbd.sd_sb.sb_locktable);
139
 
        printf("  Mounted host data = \"%s\"\n",
140
 
               get_sysfs(fs, "args/hostdata"));
141
 
        printf("  Journal number = %s\n", get_sysfs(fs, "lockstruct/jid"));
142
 
        flags = get_sysfs_uint(fs, "lockstruct/flags");
143
 
        printf("  Lock module flags = %x", flags);
144
 
        printf("\n");
145
 
        printf("  Local flocks = %s\n",
146
 
               (get_sysfs_uint(fs, "args/localflocks")) ? "TRUE" : "FALSE");
147
 
        printf("  Local caching = %s\n",
148
 
                (get_sysfs_uint(fs, "args/localcaching")) ? "TRUE" : "FALSE");
149
 
 
150
 
        /* Read the master statfs file */
151
 
        mount_gfs2_meta(&sbd);
152
 
 
153
 
        sprintf(statfs_fn, "%s/statfs", sbd.metafs_path);
154
 
        statfs_fd = open(statfs_fn, O_RDONLY);
155
 
        do_read(statfs_fd, buf, sizeof(struct gfs2_statfs_change));
156
 
        gfs2_statfs_change_in(&sc, (char *)&buf);
157
 
 
158
 
        close(statfs_fd);
159
 
 
160
 
        cleanup_metafs(&sbd);
161
 
 
162
 
        printf("\n");
163
 
        switch (output_type) {
164
 
        case OUTPUT_BLOCKS:
165
 
                printf("  %-15s%-15s%-15s%-15s%-15s\n", "Type", "Total Blocks",
166
 
                       "Used Blocks", "Free Blocks", "use%");
167
 
                break;
168
 
        case OUTPUT_K:
169
 
                printf("  %-15s%-15s%-15s%-15s%-15s\n", "Type", "Total K",
170
 
                       "Used K", "Free K", "use%");
171
 
                break;
172
 
        case OUTPUT_HUMAN:
173
 
                printf("  %-15s%-15s%-15s%-15s%-15s\n", "Type", "Total",
174
 
                       "Used", "Free", "use%");
175
 
                break;
176
 
        }
177
 
        printf("  ------------------------------------------------------------------------\n");
178
 
 
179
 
        percentage = sc.sc_total ?
180
 
                (100.0 * (sc.sc_total - sc.sc_free)) / sc.sc_total + 0.5 : 0;
181
 
        printit(sbd.sd_sb.sb_bsize, "data", sc.sc_total,
182
 
                sc.sc_total - sc.sc_free, sc.sc_free, percentage);
183
 
 
184
 
        percentage = (sc.sc_dinodes + sc.sc_free) ?
185
 
                (100.0 * sc.sc_dinodes / (sc.sc_dinodes + sc.sc_free)) + 0.5 :
186
 
                0;
187
 
        printit(sbd.sd_sb.sb_bsize, "inodes", sc.sc_dinodes + sc.sc_free,
188
 
                sc.sc_dinodes, sc.sc_free, percentage);
189
 
}
190
 
 
191
 
 
192
 
/**
193
 
 * print_df - print out information about filesystems
194
 
 * @argc:
195
 
 * @argv:
196
 
 *
197
 
 */
198
 
 
199
 
void
200
 
print_df(int argc, char **argv)
201
 
{
202
 
        if (optind < argc) {
203
 
                char buf[PATH_MAX];
204
 
 
205
 
                if (!realpath(argv[optind], buf))
206
 
                        die("can't determine real path: %s\n", strerror(errno));
207
 
 
208
 
                do_df_one(buf);
209
 
 
210
 
                return;
211
 
        }
212
 
 
213
 
        {
214
 
                FILE *file;
215
 
                char buf[256], device[256], path[256], type[256];
216
 
                int first = TRUE;
217
 
 
218
 
                file = fopen("/proc/mounts", "r");
219
 
                if (!file)
220
 
                        die("can't open /proc/mounts: %s\n", strerror(errno));
221
 
 
222
 
                while (fgets(buf, 256, file)) {
223
 
                        if (sscanf(buf, "%s %s %s", device, path, type) != 3)
224
 
                                continue;
225
 
                        if (strcmp(type, "gfs2") != 0)
226
 
                                continue;
227
 
 
228
 
                        if (first)
229
 
                                first = FALSE;
230
 
                        else
231
 
                                printf("\n");
232
 
 
233
 
                        do_df_one(path);
234
 
                }
235
 
 
236
 
                fclose(file);
237
 
        }
238
 
}