yaffs Improve yaffs direct link following
[yaffs2.git] / yaffs_bitmap.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 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 Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
21 {
22         if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
23                 T(YAFFS_TRACE_ERROR,
24                         (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
25                         blk));
26                 YBUG();
27         }
28         return dev->chunkBits +
29                 (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
30 }
31
32 void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
33 {
34         if (blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
35                         chunk < 0 || chunk >= dev->param.nChunksPerBlock) {
36                 T(YAFFS_TRACE_ERROR,
37                 (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),
38                         blk, chunk));
39                 YBUG();
40         }
41 }
42
43 void yaffs_ClearChunkBits(yaffs_Device *dev, int blk)
44 {
45         __u8 *blkBits = yaffs_BlockBits(dev, blk);
46
47         memset(blkBits, 0, dev->chunkBitmapStride);
48 }
49
50 void yaffs_ClearChunkBit(yaffs_Device *dev, int blk, int chunk)
51 {
52         __u8 *blkBits = yaffs_BlockBits(dev, blk);
53
54         yaffs_VerifyChunkBitId(dev, blk, chunk);
55
56         blkBits[chunk / 8] &= ~(1 << (chunk & 7));
57 }
58
59 void yaffs_SetChunkBit(yaffs_Device *dev, int blk, int chunk)
60 {
61         __u8 *blkBits = yaffs_BlockBits(dev, blk);
62
63         yaffs_VerifyChunkBitId(dev, blk, chunk);
64
65         blkBits[chunk / 8] |= (1 << (chunk & 7));
66 }
67
68 int yaffs_CheckChunkBit(yaffs_Device *dev, int blk, int chunk)
69 {
70         __u8 *blkBits = yaffs_BlockBits(dev, blk);
71         yaffs_VerifyChunkBitId(dev, blk, chunk);
72
73         return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
74 }
75
76 int yaffs_StillSomeChunkBits(yaffs_Device *dev, int blk)
77 {
78         __u8 *blkBits = yaffs_BlockBits(dev, blk);
79         int i;
80         for (i = 0; i < dev->chunkBitmapStride; i++) {
81                 if (*blkBits)
82                         return 1;
83                 blkBits++;
84         }
85         return 0;
86 }
87
88 int yaffs_CountChunkBits(yaffs_Device *dev, int blk)
89 {
90         __u8 *blkBits = yaffs_BlockBits(dev, blk);
91         int i;
92         int n = 0;
93         for (i = 0; i < dev->chunkBitmapStride; i++) {
94                 __u8 x = *blkBits;
95                 while (x) {
96                         if (x & 1)
97                                 n++;
98                         x >>= 1;
99                 }
100
101                 blkBits++;
102         }
103         return n;
104 }
105