Fix yaffs_endian.h for Linux
[yaffs2.git] / yaffs_bitmap.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include "yaffs_bitmap.h"
15 #include "yaffs_trace.h"
16 /*
17  * Chunk bitmap manipulations
18  */
19
20 static inline u8 *yaffs_block_bits(struct yaffs_dev *dev, int blk)
21 {
22         if (blk < (int)dev->internal_start_block ||
23             blk > (int)dev->internal_end_block) {
24                 yaffs_trace(YAFFS_TRACE_ERROR,
25                         "BlockBits block %d is not valid",
26                         blk);
27                 BUG();
28         }
29         return dev->chunk_bits +
30             (dev->chunk_bit_stride * (blk - dev->internal_start_block));
31 }
32
33 void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk)
34 {
35         if (blk < (int)dev->internal_start_block ||
36             blk > (int)dev->internal_end_block ||
37             chunk < 0 || chunk >= (int)dev->param.chunks_per_block) {
38                 yaffs_trace(YAFFS_TRACE_ERROR,
39                         "Chunk Id (%d:%d) invalid",
40                         blk, chunk);
41                 BUG();
42         }
43 }
44
45 void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk)
46 {
47         u8 *blk_bits = yaffs_block_bits(dev, blk);
48
49         memset(blk_bits, 0, dev->chunk_bit_stride);
50 }
51
52 void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
53 {
54         u8 *blk_bits = yaffs_block_bits(dev, blk);
55
56         yaffs_verify_chunk_bit_id(dev, blk, chunk);
57         blk_bits[chunk / 8] &= ~(1 << (chunk & 7));
58 }
59
60 void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
61 {
62         u8 *blk_bits = yaffs_block_bits(dev, blk);
63
64         yaffs_verify_chunk_bit_id(dev, blk, chunk);
65         blk_bits[chunk / 8] |= (1 << (chunk & 7));
66 }
67
68 int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
69 {
70         u8 *blk_bits = yaffs_block_bits(dev, blk);
71
72         yaffs_verify_chunk_bit_id(dev, blk, chunk);
73         return (blk_bits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
74 }
75
76 int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk)
77 {
78         u8 *blk_bits = yaffs_block_bits(dev, blk);
79         int i;
80
81         for (i = 0; i < dev->chunk_bit_stride; i++) {
82                 if (*blk_bits)
83                         return 1;
84                 blk_bits++;
85         }
86         return 0;
87 }
88
89 int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk)
90 {
91         u8 *blk_bits = yaffs_block_bits(dev, blk);
92         int i;
93         int n = 0;
94
95         for (i = 0; i < dev->chunk_bit_stride; i++, blk_bits++)
96                 n += hweight8(*blk_bits);
97
98         return n;
99 }