~wb-munzinger/+junk/ocfs2-tools

« back to all changes in this revision

Viewing changes to o2info/libo2info.c

  • Committer: David Weber
  • Date: 2012-01-30 08:42:00 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: wb@munzinger.de-20120130084200-c8cy478mu9fk7tkf
Import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c; c-basic-offset: 8; -*-
 
2
 * vim: noexpandtab sw=8 ts=8 sts=0:
 
3
 *
 
4
 * libo2info.c
 
5
 *
 
6
 * Shared routines for the ocfs2 o2info utility
 
7
 *
 
8
 * Copyright (C) 2010 Oracle.  All rights reserved.
 
9
 *
 
10
 * This program is free software; you can redistribute it and/or
 
11
 * modify it under the terms of the GNU General Public
 
12
 * License version 2 as published by the Free Software Foundation.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
 * General Public License for more details.
 
18
 */
 
19
 
 
20
#define _XOPEN_SOURCE 600
 
21
#define _LARGEFILE64_SOURCE
 
22
#define _GNU_SOURCE
 
23
 
 
24
#include <errno.h>
 
25
#include <inttypes.h>
 
26
#include <sys/ioctl.h>
 
27
#include <linux/fs.h>
 
28
 
 
29
#include "ocfs2/ocfs2.h"
 
30
#include "ocfs2/bitops.h"
 
31
#include "ocfs2-kernel/fiemap.h"
 
32
#include "tools-internal/verbose.h"
 
33
#include "libo2info.h"
 
34
 
 
35
int o2info_get_fs_features(ocfs2_filesys *fs, struct o2info_fs_features *ofs)
 
36
{
 
37
        int rc = 0;
 
38
        struct ocfs2_super_block *sb = NULL;
 
39
 
 
40
        memset(ofs, 0, sizeof(*ofs));
 
41
 
 
42
        sb = OCFS2_RAW_SB(fs->fs_super);
 
43
        ofs->compat = sb->s_feature_compat;
 
44
        ofs->incompat = sb->s_feature_incompat;
 
45
        ofs->rocompat = sb->s_feature_ro_compat;
 
46
 
 
47
        return rc;
 
48
}
 
49
 
 
50
int o2info_get_volinfo(ocfs2_filesys *fs, struct o2info_volinfo *vf)
 
51
{
 
52
        int rc = 0;
 
53
        struct ocfs2_super_block *sb = NULL;
 
54
 
 
55
        memset(vf, 0, sizeof(*vf));
 
56
 
 
57
        sb = OCFS2_RAW_SB(fs->fs_super);
 
58
        vf->blocksize = fs->fs_blocksize;
 
59
        vf->clustersize = fs->fs_clustersize;
 
60
        vf->maxslots = sb->s_max_slots;
 
61
        memcpy(vf->label, sb->s_label, OCFS2_MAX_VOL_LABEL_LEN);
 
62
        memcpy(vf->uuid_str, fs->uuid_str, OCFS2_TEXT_UUID_LEN + 1);
 
63
        rc = o2info_get_fs_features(fs, &(vf->ofs));
 
64
 
 
65
        return rc;
 
66
}
 
67
 
 
68
int o2info_get_mkfs(ocfs2_filesys *fs, struct o2info_mkfs *oms)
 
69
{
 
70
        errcode_t err;
 
71
        uint64_t blkno;
 
72
        char *buf = NULL;
 
73
        struct ocfs2_dinode *di = NULL;
 
74
 
 
75
        memset(oms, 0, sizeof(*oms));
 
76
 
 
77
        err = ocfs2_malloc_block(fs->fs_io, &buf);
 
78
        if (err) {
 
79
                tcom_err(err, "while allocating buffer");
 
80
                goto out;
 
81
        }
 
82
 
 
83
        err = ocfs2_lookup_system_inode(fs, JOURNAL_SYSTEM_INODE, 0, &blkno);
 
84
        if (err) {
 
85
                tcom_err(err, "while looking up journal system inode");
 
86
                goto out;
 
87
        }
 
88
 
 
89
        err = ocfs2_read_inode(fs, blkno, buf);
 
90
        if (err) {
 
91
                tcom_err(err, "while reading journal system inode");
 
92
                goto out;
 
93
        }
 
94
 
 
95
        di = (struct ocfs2_dinode *)buf;
 
96
        oms->journal_size = di->i_size;
 
97
        err = o2info_get_volinfo(fs, &(oms->ovf));
 
98
 
 
99
out:
 
100
        if (buf)
 
101
                ocfs2_free(&buf);
 
102
 
 
103
        return err;
 
104
}
 
105
 
 
106
int o2info_get_freeinode(ocfs2_filesys *fs, struct o2info_freeinode *ofi)
 
107
{
 
108
 
 
109
        int ret = 0, i, j;
 
110
        char *block = NULL;
 
111
        uint64_t inode_alloc;
 
112
 
 
113
        struct ocfs2_dinode *dinode_alloc = NULL;
 
114
        struct ocfs2_chain_list *cl = NULL;
 
115
        struct ocfs2_chain_rec *rec = NULL;
 
116
        struct ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);
 
117
 
 
118
        ofi->slotnum = sb->s_max_slots;
 
119
 
 
120
        ret = ocfs2_malloc_block(fs->fs_io, &block);
 
121
        if (ret) {
 
122
                tcom_err(ret, "while allocating block buffer");
 
123
                goto out;
 
124
        }
 
125
 
 
126
        dinode_alloc = (struct ocfs2_dinode *)block;
 
127
 
 
128
        for (i = 0; i < ofi->slotnum; i++) {
 
129
 
 
130
                ofi->fi[i].total = ofi->fi[i].free = 0;
 
131
 
 
132
                ret = ocfs2_lookup_system_inode(fs, INODE_ALLOC_SYSTEM_INODE,
 
133
                                                i, &inode_alloc);
 
134
                if (ret) {
 
135
                        tcom_err(ret, "while looking up the global"
 
136
                                 " bitmap inode");
 
137
                        goto out;
 
138
                }
 
139
 
 
140
                ret = ocfs2_read_inode(fs, inode_alloc, (char *)dinode_alloc);
 
141
                if (ret) {
 
142
                        tcom_err(ret, "reading global_bitmap inode "
 
143
                                 "%"PRIu64" for stats", inode_alloc);
 
144
                        goto out;
 
145
                }
 
146
 
 
147
                cl = &(dinode_alloc->id2.i_chain);
 
148
 
 
149
                for (j = 0; j < cl->cl_next_free_rec; j++) {
 
150
                        rec = &(cl->cl_recs[j]);
 
151
                        ofi->fi[i].total += rec->c_total;
 
152
                        ofi->fi[i].free += rec->c_free;
 
153
                }
 
154
        }
 
155
out:
 
156
        if (block)
 
157
                ocfs2_free(&block);
 
158
 
 
159
        return ret;
 
160
}
 
161
 
 
162
static int ul_log2(unsigned long arg)
 
163
{
 
164
        unsigned int i = 0;
 
165
 
 
166
        arg >>= 1;
 
167
        while (arg) {
 
168
                i++;
 
169
                arg >>= 1;
 
170
        }
 
171
 
 
172
        return i;
 
173
}
 
174
 
 
175
static void o2info_update_freefrag_stats(struct o2info_freefrag *off,
 
176
                                         unsigned int chunksize)
 
177
{
 
178
        int index;
 
179
 
 
180
        index = ul_log2(chunksize);
 
181
        if (index >= OCFS2_INFO_MAX_HIST)
 
182
                index = OCFS2_INFO_MAX_HIST - 1;
 
183
 
 
184
        off->histogram.fc_chunks[index]++;
 
185
        off->histogram.fc_clusters[index] += chunksize;
 
186
 
 
187
        if (chunksize > off->max)
 
188
                off->max = chunksize;
 
189
 
 
190
        if (chunksize < off->min)
 
191
                off->min = chunksize;
 
192
 
 
193
        off->avg += chunksize;
 
194
        off->free_chunks_real++;
 
195
}
 
196
 
 
197
static int o2info_scan_global_bitmap_chain(ocfs2_filesys *fs,
 
198
                                           struct ocfs2_chain_rec *rec,
 
199
                                           struct o2info_freefrag *off)
 
200
{
 
201
        int ret = 0, used;
 
202
        uint64_t blkno;
 
203
 
 
204
        char *block = NULL;
 
205
        struct ocfs2_group_desc *bg = NULL;
 
206
 
 
207
        unsigned int max_bits, num_clusters;
 
208
        unsigned int offset = 0, cluster, chunk;
 
209
        unsigned int chunk_free, last_chunksize = 0;
 
210
 
 
211
        if (!rec->c_free)
 
212
                goto out;
 
213
 
 
214
        ret = ocfs2_malloc_block(fs->fs_io, &block);
 
215
        if (ret) {
 
216
                tcom_err(ret, "while allocating block buffer");
 
217
                goto out;
 
218
        }
 
219
 
 
220
        do {
 
221
                if (!bg)
 
222
                        blkno = rec->c_blkno;
 
223
                else
 
224
                        blkno = bg->bg_next_group;
 
225
 
 
226
                ret = ocfs2_read_blocks(fs, blkno, 1, block);
 
227
                if (ret < 0) {
 
228
                        tcom_err(ret, "while reading group descriptor "
 
229
                                 "%"PRIu64" for stats", blkno);
 
230
                        goto out;
 
231
                }
 
232
 
 
233
                bg = (struct ocfs2_group_desc *)block;
 
234
 
 
235
                if (!bg->bg_free_bits_count)
 
236
                        continue;
 
237
 
 
238
                max_bits = bg->bg_bits;
 
239
                offset = 0;
 
240
 
 
241
                for (chunk = 0; chunk < off->chunks_in_group; chunk++) {
 
242
 
 
243
                        /*
 
244
                         * last chunk may be not an entire one.
 
245
                         */
 
246
                        if ((offset + off->clusters_in_chunk) > max_bits)
 
247
                                num_clusters = max_bits - offset;
 
248
                        else
 
249
                                num_clusters = off->clusters_in_chunk;
 
250
 
 
251
                        chunk_free = 0;
 
252
 
 
253
                        for (cluster = 0; cluster < num_clusters; cluster++) {
 
254
                                used = ocfs2_test_bit(offset,
 
255
                                                (unsigned long *)bg->bg_bitmap);
 
256
                                if (!used) {
 
257
                                        last_chunksize++;
 
258
                                        chunk_free++;
 
259
                                }
 
260
 
 
261
                                if (used && (last_chunksize)) {
 
262
                                        o2info_update_freefrag_stats(off,
 
263
                                                                last_chunksize);
 
264
                                        last_chunksize = 0;
 
265
                                }
 
266
 
 
267
                                offset++;
 
268
                        }
 
269
 
 
270
                        if (chunk_free == off->clusters_in_chunk)
 
271
                                off->free_chunks++;
 
272
                }
 
273
 
 
274
                /*
 
275
                 * need to update the info of last free chunk.
 
276
                 */
 
277
                if (last_chunksize)
 
278
                        o2info_update_freefrag_stats(off, last_chunksize);
 
279
 
 
280
        } while (bg->bg_next_group);
 
281
 
 
282
out:
 
283
        if (block)
 
284
                ocfs2_free(&block);
 
285
 
 
286
        return ret;
 
287
}
 
288
 
 
289
static int o2info_scan_global_bitmap(ocfs2_filesys *fs,
 
290
                                     struct ocfs2_chain_list *cl,
 
291
                                     struct o2info_freefrag *off)
 
292
{
 
293
        int ret = 0, i;
 
294
        struct ocfs2_chain_rec *rec = NULL;
 
295
 
 
296
        off->chunks_in_group = (cl->cl_cpg / off->clusters_in_chunk) + 1;
 
297
 
 
298
        for (i = 0; i < cl->cl_next_free_rec; i++) {
 
299
                rec = &(cl->cl_recs[i]);
 
300
                ret = o2info_scan_global_bitmap_chain(fs, rec, off);
 
301
                if (ret)
 
302
                        return ret;
 
303
        }
 
304
 
 
305
        return ret;
 
306
}
 
307
 
 
308
int o2info_get_freefrag(ocfs2_filesys *fs, struct o2info_freefrag *off)
 
309
{
 
310
        int ret = 0;
 
311
        char *block = NULL;
 
312
 
 
313
        uint64_t gb_inode;
 
314
        struct ocfs2_dinode *gb_di = NULL;
 
315
        struct ocfs2_chain_list *cl = NULL;
 
316
 
 
317
        ret = ocfs2_malloc_block(fs->fs_io, &block);
 
318
        if (ret) {
 
319
                tcom_err(ret, "while allocating block buffer");
 
320
                goto out;
 
321
        }
 
322
 
 
323
        gb_di = (struct ocfs2_dinode *)block;
 
324
 
 
325
        ret = ocfs2_lookup_system_inode(fs, GLOBAL_BITMAP_SYSTEM_INODE,
 
326
                                        0, &gb_inode);
 
327
        if (ret) {
 
328
                tcom_err(ret, "while looking up the global bitmap inode");
 
329
                goto out;
 
330
        }
 
331
 
 
332
        ret = ocfs2_read_inode(fs, gb_inode, (char *)gb_di);
 
333
        if (ret) {
 
334
                tcom_err(ret, "reading global_bitmap inode "
 
335
                         "%"PRIu64" for stats", gb_inode);
 
336
                goto out;
 
337
        }
 
338
 
 
339
        off->clusters = gb_di->id1.bitmap1.i_total;
 
340
        off->free_clusters = gb_di->id1.bitmap1.i_total -
 
341
                                gb_di->id1.bitmap1.i_used;
 
342
 
 
343
        off->total_chunks = (off->clusters + off->clusters_in_chunk) >>
 
344
                                (off->chunkbits - off->clustersize_bits);
 
345
        cl = &(gb_di->id2.i_chain);
 
346
 
 
347
        ret = o2info_scan_global_bitmap(fs, cl, off);
 
348
        if (ret)
 
349
                goto out;
 
350
 
 
351
        if (off->free_chunks_real) {
 
352
                off->min <<= (off->clustersize_bits - 10);
 
353
                off->max <<= (off->clustersize_bits - 10);
 
354
                off->avg /= off->free_chunks_real;
 
355
                off->avg <<= (off->clustersize_bits - 10);
 
356
        }
 
357
 
 
358
out:
 
359
        if (block)
 
360
                ocfs2_free(&block);
 
361
 
 
362
        return ret;
 
363
}
 
364
 
 
365
static int figure_extents(int fd, uint32_t *num, int flags)
 
366
{
 
367
        int ret;
 
368
        static struct fiemap fiemap;
 
369
 
 
370
        fiemap.fm_start = 0ULL;
 
371
        fiemap.fm_length = FIEMAP_MAX_OFFSET;
 
372
 
 
373
        if (flags & FIEMAP_FLAG_XATTR)
 
374
                fiemap.fm_flags = FIEMAP_FLAG_XATTR;
 
375
 
 
376
        fiemap.fm_extent_count = 0;
 
377
        ret = ioctl(fd, FS_IOC_FIEMAP, &fiemap);
 
378
        if (ret < 0) {
 
379
                ret = errno;
 
380
                tcom_err(ret, "fiemap get count error");
 
381
                return -1;
 
382
        }
 
383
 
 
384
        *num = fiemap.fm_mapped_extents;
 
385
 
 
386
        return 0;
 
387
}
 
388
 
 
389
static uint32_t clusters_in_bytes(uint32_t clustersize, uint32_t bytes)
 
390
{
 
391
        uint64_t ret = bytes + clustersize - 1;
 
392
 
 
393
        if (ret < bytes)
 
394
                ret = UINT64_MAX;
 
395
 
 
396
        ret = ret >> ul_log2(clustersize);
 
397
        if (ret > UINT32_MAX)
 
398
                ret = UINT32_MAX;
 
399
 
 
400
        return (uint32_t)ret;
 
401
}
 
402
 
 
403
static int do_fiemap(int fd, int flags, struct o2info_fiemap *ofp)
 
404
{
 
405
        char buf[4096];
 
406
 
 
407
        int ret = 0, last = 0;
 
408
        int cluster_shift = 0, blk_shift = 0;
 
409
        int count = (sizeof(buf) - sizeof(struct fiemap)) /
 
410
                     sizeof(struct fiemap_extent);
 
411
 
 
412
        struct fiemap *fiemap = (struct fiemap *)buf;
 
413
        struct fiemap_extent *fm_ext = &fiemap->fm_extents[0];
 
414
        uint32_t num_extents = 0, extents_got = 0, i;
 
415
 
 
416
        uint32_t prev_start = 0, prev_len = 0;
 
417
        uint32_t start = 0, len = 0, phy_pos = 0;
 
418
 
 
419
        if (ofp->clustersize)
 
420
                cluster_shift = ul_log2(ofp->clustersize);
 
421
 
 
422
        if (ofp->blocksize)
 
423
                blk_shift = ul_log2(ofp->blocksize);
 
424
 
 
425
        memset(fiemap, 0, sizeof(*fiemap));
 
426
 
 
427
        ret = figure_extents(fd, &num_extents, 0);
 
428
        if (ret)
 
429
                return -1;
 
430
 
 
431
        if (flags & FIEMAP_FLAG_XATTR)
 
432
                fiemap->fm_flags = FIEMAP_FLAG_XATTR;
 
433
        else
 
434
                fiemap->fm_flags = flags;
 
435
 
 
436
        do {
 
437
                fiemap->fm_length = ~0ULL;
 
438
                fiemap->fm_extent_count = count;
 
439
 
 
440
                ret = ioctl(fd, FS_IOC_FIEMAP, (unsigned long)fiemap);
 
441
                if (ret < 0) {
 
442
                        ret = errno;
 
443
                        if (errno == EBADR) {
 
444
                                fprintf(stderr, "fiemap failed with unsupported"
 
445
                                        " flags %x\n", fiemap->fm_flags);
 
446
                        } else
 
447
                                fprintf(stderr, "fiemap error: %d, %s\n",
 
448
                                        ret, strerror(ret));
 
449
                        return -1;
 
450
                }
 
451
 
 
452
                if (!fiemap->fm_mapped_extents)
 
453
                        break;
 
454
 
 
455
                for (i = 0; i < fiemap->fm_mapped_extents; i++) {
 
456
 
 
457
                        start = fm_ext[i].fe_logical >> cluster_shift;
 
458
                        len = fm_ext[i].fe_length >> cluster_shift;
 
459
                        phy_pos = fm_ext[i].fe_physical >> blk_shift;
 
460
 
 
461
                        if (fiemap->fm_flags & FIEMAP_FLAG_XATTR) {
 
462
                                ofp->xattr += len;
 
463
                        } else {
 
464
                                if (fm_ext[i].fe_flags &
 
465
                                    FIEMAP_EXTENT_UNWRITTEN)
 
466
                                        ofp->unwrittens += len;
 
467
 
 
468
                                if (fm_ext[i].fe_flags & FIEMAP_EXTENT_SHARED)
 
469
                                        ofp->shared += len;
 
470
 
 
471
                                if ((prev_start + prev_len) < start)
 
472
                                        ofp->holes += start - prev_start -
 
473
                                                      prev_len;
 
474
                        }
 
475
 
 
476
                        if (fm_ext[i].fe_flags & FIEMAP_EXTENT_LAST)
 
477
                                last = 1;
 
478
 
 
479
                        prev_start = start;
 
480
                        prev_len = len;
 
481
 
 
482
                        extents_got++;
 
483
                        ofp->clusters += len;
 
484
                }
 
485
 
 
486
                fiemap->fm_start = (fm_ext[i-1].fe_logical +
 
487
                                    fm_ext[i-1].fe_length);
 
488
        } while (!last);
 
489
 
 
490
        if (extents_got != num_extents) {
 
491
                fprintf(stderr, "Got wrong extents number, expected:%lu, "
 
492
                        "got:%lu\n", num_extents, extents_got);
 
493
                return -1;
 
494
        }
 
495
 
 
496
        if (flags & FIEMAP_FLAG_XATTR)
 
497
                ofp->num_extents_xattr = num_extents;
 
498
        else
 
499
                ofp->num_extents = num_extents;
 
500
 
 
501
        return ret;
 
502
}
 
503
 
 
504
int o2info_get_fiemap(int fd, int flags, struct o2info_fiemap *ofp)
 
505
{
 
506
        int ret = 0;
 
507
 
 
508
        ret = do_fiemap(fd, flags, ofp);
 
509
        if (ret)
 
510
                return ret;
 
511
 
 
512
        if ((ofp->clusters > 1) && ofp->num_extents) {
 
513
                float e = ofp->num_extents, c = ofp->clusters;
 
514
                int clusters_per_mb = clusters_in_bytes(ofp->clustersize,
 
515
                                                        OCFS2_MAX_CLUSTERSIZE);
 
516
                ofp->frag = 100 * (e / c);
 
517
                ofp->score = ofp->frag * clusters_per_mb;
 
518
        }
 
519
 
 
520
        return ret;
 
521
}