Blame view

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