This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / ntfs / bitmap.c
1 /*
2  * bitmap.c - NTFS kernel bitmap handling.  Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2004 Anton Altaparmakov
5  *
6  * This program/include file is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program/include file is distributed in the hope that it will be
12  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program (in the main directory of the Linux-NTFS
18  * distribution in the file COPYING); if not, write to the Free Software
19  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #ifdef NTFS_RW
23
24 #include <linux/pagemap.h>
25
26 #include "bitmap.h"
27 #include "debug.h"
28 #include "ntfs.h"
29
30 /**
31  * __ntfs_bitmap_set_bits_in_run - set a run of bits in a bitmap to a value
32  * @vi:                 vfs inode describing the bitmap
33  * @start_bit:          first bit to set
34  * @count:              number of bits to set
35  * @value:              value to set the bits to (i.e. 0 or 1)
36  * @is_rollback:        if TRUE this is a rollback operation
37  *
38  * Set @count bits starting at bit @start_bit in the bitmap described by the
39  * vfs inode @vi to @value, where @value is either 0 or 1.
40  *
41  * @is_rollback should always be FALSE, it is for internal use to rollback
42  * errors.  You probably want to use ntfs_bitmap_set_bits_in_run() instead.
43  *
44  * Return 0 on success and -errno on error.
45  */
46 int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
47                 const s64 count, const u8 value, const BOOL is_rollback)
48 {
49         s64 cnt = count;
50         pgoff_t index, end_index;
51         struct address_space *mapping;
52         struct page *page;
53         u8 *kaddr;
54         int pos, len;
55         u8 bit;
56
57         BUG_ON(!vi);
58         ntfs_debug("Entering for i_ino 0x%lx, start_bit 0x%llx, count 0x%llx, "
59                         "value %u.%s", vi->i_ino, (unsigned long long)start_bit,
60                         (unsigned long long)cnt, (unsigned int)value,
61                         is_rollback ? " (rollback)" : "");
62         BUG_ON(start_bit < 0);
63         BUG_ON(cnt < 0);
64         BUG_ON(value > 1);
65         /*
66          * Calculate the indices for the pages containing the first and last
67          * bits, i.e. @start_bit and @start_bit + @cnt - 1, respectively.
68          */
69         index = start_bit >> (3 + PAGE_CACHE_SHIFT);
70         end_index = (start_bit + cnt - 1) >> (3 + PAGE_CACHE_SHIFT);
71
72         /* Get the page containing the first bit (@start_bit). */
73         mapping = vi->i_mapping;
74         page = ntfs_map_page(mapping, index);
75         if (IS_ERR(page)) {
76                 if (!is_rollback)
77                         ntfs_error(vi->i_sb, "Failed to map first page (error "
78                                         "%li), aborting.", PTR_ERR(page));
79                 return PTR_ERR(page);
80         }
81         kaddr = page_address(page);
82
83         /* Set @pos to the position of the byte containing @start_bit. */
84         pos = (start_bit >> 3) & ~PAGE_CACHE_MASK;
85
86         /* Calculate the position of @start_bit in the first byte. */
87         bit = start_bit & 7;
88
89         /* If the first byte is partial, modify the appropriate bits in it. */
90         if (bit) {
91                 u8 *byte = kaddr + pos;
92                 while ((bit & 7) && cnt--) {
93                         if (value)
94                                 *byte |= 1 << bit++;
95                         else
96                                 *byte &= ~(1 << bit++);
97                 }
98                 /* If we are done, unmap the page and return success. */
99                 if (!cnt)
100                         goto done;
101
102                 /* Update @pos to the new position. */
103                 pos++;
104         }
105         /*
106          * Depending on @value, modify all remaining whole bytes in the page up
107          * to @cnt.
108          */
109         len = min_t(s64, cnt >> 3, PAGE_CACHE_SIZE - pos);
110         memset(kaddr + pos, value ? 0xff : 0, len);
111         cnt -= len << 3;
112
113         /* Update @len to point to the first not-done byte in the page. */
114         if (cnt < 8)
115                 len += pos;
116
117         /* If we are not in the last page, deal with all subsequent pages. */
118         while (index < end_index) {
119                 BUG_ON(cnt <= 0);
120
121                 /* Update @index and get the next page. */
122                 flush_dcache_page(page);
123                 set_page_dirty(page);
124                 ntfs_unmap_page(page);
125                 page = ntfs_map_page(mapping, ++index);
126                 if (IS_ERR(page))
127                         goto rollback;
128                 kaddr = page_address(page);
129                 /*
130                  * Depending on @value, modify all remaining whole bytes in the
131                  * page up to @cnt.
132                  */
133                 len = min_t(s64, cnt >> 3, PAGE_CACHE_SIZE);
134                 memset(kaddr, value ? 0xff : 0, len);
135                 cnt -= len << 3;
136         }
137         /*
138          * The currently mapped page is the last one.  If the last byte is
139          * partial, modify the appropriate bits in it.  Note, @len is the
140          * position of the last byte inside the page.
141          */
142         if (cnt) {
143                 u8 *byte;
144
145                 BUG_ON(cnt > 7);
146
147                 bit = cnt;
148                 byte = kaddr + len;
149                 while (bit--) {
150                         if (value)
151                                 *byte |= 1 << bit;
152                         else
153                                 *byte &= ~(1 << bit);
154                 }
155         }
156 done:
157         /* We are done.  Unmap the page and return success. */
158         flush_dcache_page(page);
159         set_page_dirty(page);
160         ntfs_unmap_page(page);
161         ntfs_debug("Done.");
162         return 0;
163 rollback:
164         /*
165          * Current state:
166          *      - no pages are mapped
167          *      - @count - @cnt is the number of bits that have been modified
168          */
169         if (is_rollback)
170                 return PTR_ERR(page);
171         if (count != cnt)
172                 pos = __ntfs_bitmap_set_bits_in_run(vi, start_bit, count - cnt,
173                                 value ? 0 : 1, TRUE);
174         else
175                 pos = 0;
176         if (!pos) {
177                 /* Rollback was successful. */
178                 ntfs_error(vi->i_sb, "Failed to map subsequent page (error "
179                                 "%li), aborting.", PTR_ERR(page));
180         } else {
181                 /* Rollback failed. */
182                 ntfs_error(vi->i_sb, "Failed to map subsequent page (error "
183                                 "%li) and rollback failed (error %i).  "
184                                 "Aborting and leaving inconsistent metadata.  "
185                                 "Unmount and run chkdsk.", PTR_ERR(page), pos);
186                 NVolSetErrors(NTFS_SB(vi->i_sb));
187         }
188         return PTR_ERR(page);
189 }
190
191 #endif /* NTFS_RW */