xref: /OK3568_Linux_fs/kernel/fs/ntfs/bitmap.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * bitmap.c - NTFS kernel bitmap handling.  Part of the Linux-NTFS project.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2004-2005 Anton Altaparmakov
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #ifdef NTFS_RW
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/pagemap.h>
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include "bitmap.h"
13*4882a593Smuzhiyun #include "debug.h"
14*4882a593Smuzhiyun #include "aops.h"
15*4882a593Smuzhiyun #include "ntfs.h"
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun  * __ntfs_bitmap_set_bits_in_run - set a run of bits in a bitmap to a value
19*4882a593Smuzhiyun  * @vi:			vfs inode describing the bitmap
20*4882a593Smuzhiyun  * @start_bit:		first bit to set
21*4882a593Smuzhiyun  * @count:		number of bits to set
22*4882a593Smuzhiyun  * @value:		value to set the bits to (i.e. 0 or 1)
23*4882a593Smuzhiyun  * @is_rollback:	if 'true' this is a rollback operation
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * Set @count bits starting at bit @start_bit in the bitmap described by the
26*4882a593Smuzhiyun  * vfs inode @vi to @value, where @value is either 0 or 1.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * @is_rollback should always be 'false', it is for internal use to rollback
29*4882a593Smuzhiyun  * errors.  You probably want to use ntfs_bitmap_set_bits_in_run() instead.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * Return 0 on success and -errno on error.
32*4882a593Smuzhiyun  */
__ntfs_bitmap_set_bits_in_run(struct inode * vi,const s64 start_bit,const s64 count,const u8 value,const bool is_rollback)33*4882a593Smuzhiyun int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
34*4882a593Smuzhiyun 		const s64 count, const u8 value, const bool is_rollback)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	s64 cnt = count;
37*4882a593Smuzhiyun 	pgoff_t index, end_index;
38*4882a593Smuzhiyun 	struct address_space *mapping;
39*4882a593Smuzhiyun 	struct page *page;
40*4882a593Smuzhiyun 	u8 *kaddr;
41*4882a593Smuzhiyun 	int pos, len;
42*4882a593Smuzhiyun 	u8 bit;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	BUG_ON(!vi);
45*4882a593Smuzhiyun 	ntfs_debug("Entering for i_ino 0x%lx, start_bit 0x%llx, count 0x%llx, "
46*4882a593Smuzhiyun 			"value %u.%s", vi->i_ino, (unsigned long long)start_bit,
47*4882a593Smuzhiyun 			(unsigned long long)cnt, (unsigned int)value,
48*4882a593Smuzhiyun 			is_rollback ? " (rollback)" : "");
49*4882a593Smuzhiyun 	BUG_ON(start_bit < 0);
50*4882a593Smuzhiyun 	BUG_ON(cnt < 0);
51*4882a593Smuzhiyun 	BUG_ON(value > 1);
52*4882a593Smuzhiyun 	/*
53*4882a593Smuzhiyun 	 * Calculate the indices for the pages containing the first and last
54*4882a593Smuzhiyun 	 * bits, i.e. @start_bit and @start_bit + @cnt - 1, respectively.
55*4882a593Smuzhiyun 	 */
56*4882a593Smuzhiyun 	index = start_bit >> (3 + PAGE_SHIFT);
57*4882a593Smuzhiyun 	end_index = (start_bit + cnt - 1) >> (3 + PAGE_SHIFT);
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	/* Get the page containing the first bit (@start_bit). */
60*4882a593Smuzhiyun 	mapping = vi->i_mapping;
61*4882a593Smuzhiyun 	page = ntfs_map_page(mapping, index);
62*4882a593Smuzhiyun 	if (IS_ERR(page)) {
63*4882a593Smuzhiyun 		if (!is_rollback)
64*4882a593Smuzhiyun 			ntfs_error(vi->i_sb, "Failed to map first page (error "
65*4882a593Smuzhiyun 					"%li), aborting.", PTR_ERR(page));
66*4882a593Smuzhiyun 		return PTR_ERR(page);
67*4882a593Smuzhiyun 	}
68*4882a593Smuzhiyun 	kaddr = page_address(page);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	/* Set @pos to the position of the byte containing @start_bit. */
71*4882a593Smuzhiyun 	pos = (start_bit >> 3) & ~PAGE_MASK;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	/* Calculate the position of @start_bit in the first byte. */
74*4882a593Smuzhiyun 	bit = start_bit & 7;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	/* If the first byte is partial, modify the appropriate bits in it. */
77*4882a593Smuzhiyun 	if (bit) {
78*4882a593Smuzhiyun 		u8 *byte = kaddr + pos;
79*4882a593Smuzhiyun 		while ((bit & 7) && cnt) {
80*4882a593Smuzhiyun 			cnt--;
81*4882a593Smuzhiyun 			if (value)
82*4882a593Smuzhiyun 				*byte |= 1 << bit++;
83*4882a593Smuzhiyun 			else
84*4882a593Smuzhiyun 				*byte &= ~(1 << bit++);
85*4882a593Smuzhiyun 		}
86*4882a593Smuzhiyun 		/* If we are done, unmap the page and return success. */
87*4882a593Smuzhiyun 		if (!cnt)
88*4882a593Smuzhiyun 			goto done;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 		/* Update @pos to the new position. */
91*4882a593Smuzhiyun 		pos++;
92*4882a593Smuzhiyun 	}
93*4882a593Smuzhiyun 	/*
94*4882a593Smuzhiyun 	 * Depending on @value, modify all remaining whole bytes in the page up
95*4882a593Smuzhiyun 	 * to @cnt.
96*4882a593Smuzhiyun 	 */
97*4882a593Smuzhiyun 	len = min_t(s64, cnt >> 3, PAGE_SIZE - pos);
98*4882a593Smuzhiyun 	memset(kaddr + pos, value ? 0xff : 0, len);
99*4882a593Smuzhiyun 	cnt -= len << 3;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	/* Update @len to point to the first not-done byte in the page. */
102*4882a593Smuzhiyun 	if (cnt < 8)
103*4882a593Smuzhiyun 		len += pos;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	/* If we are not in the last page, deal with all subsequent pages. */
106*4882a593Smuzhiyun 	while (index < end_index) {
107*4882a593Smuzhiyun 		BUG_ON(cnt <= 0);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 		/* Update @index and get the next page. */
110*4882a593Smuzhiyun 		flush_dcache_page(page);
111*4882a593Smuzhiyun 		set_page_dirty(page);
112*4882a593Smuzhiyun 		ntfs_unmap_page(page);
113*4882a593Smuzhiyun 		page = ntfs_map_page(mapping, ++index);
114*4882a593Smuzhiyun 		if (IS_ERR(page))
115*4882a593Smuzhiyun 			goto rollback;
116*4882a593Smuzhiyun 		kaddr = page_address(page);
117*4882a593Smuzhiyun 		/*
118*4882a593Smuzhiyun 		 * Depending on @value, modify all remaining whole bytes in the
119*4882a593Smuzhiyun 		 * page up to @cnt.
120*4882a593Smuzhiyun 		 */
121*4882a593Smuzhiyun 		len = min_t(s64, cnt >> 3, PAGE_SIZE);
122*4882a593Smuzhiyun 		memset(kaddr, value ? 0xff : 0, len);
123*4882a593Smuzhiyun 		cnt -= len << 3;
124*4882a593Smuzhiyun 	}
125*4882a593Smuzhiyun 	/*
126*4882a593Smuzhiyun 	 * The currently mapped page is the last one.  If the last byte is
127*4882a593Smuzhiyun 	 * partial, modify the appropriate bits in it.  Note, @len is the
128*4882a593Smuzhiyun 	 * position of the last byte inside the page.
129*4882a593Smuzhiyun 	 */
130*4882a593Smuzhiyun 	if (cnt) {
131*4882a593Smuzhiyun 		u8 *byte;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 		BUG_ON(cnt > 7);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 		bit = cnt;
136*4882a593Smuzhiyun 		byte = kaddr + len;
137*4882a593Smuzhiyun 		while (bit--) {
138*4882a593Smuzhiyun 			if (value)
139*4882a593Smuzhiyun 				*byte |= 1 << bit;
140*4882a593Smuzhiyun 			else
141*4882a593Smuzhiyun 				*byte &= ~(1 << bit);
142*4882a593Smuzhiyun 		}
143*4882a593Smuzhiyun 	}
144*4882a593Smuzhiyun done:
145*4882a593Smuzhiyun 	/* We are done.  Unmap the page and return success. */
146*4882a593Smuzhiyun 	flush_dcache_page(page);
147*4882a593Smuzhiyun 	set_page_dirty(page);
148*4882a593Smuzhiyun 	ntfs_unmap_page(page);
149*4882a593Smuzhiyun 	ntfs_debug("Done.");
150*4882a593Smuzhiyun 	return 0;
151*4882a593Smuzhiyun rollback:
152*4882a593Smuzhiyun 	/*
153*4882a593Smuzhiyun 	 * Current state:
154*4882a593Smuzhiyun 	 *	- no pages are mapped
155*4882a593Smuzhiyun 	 *	- @count - @cnt is the number of bits that have been modified
156*4882a593Smuzhiyun 	 */
157*4882a593Smuzhiyun 	if (is_rollback)
158*4882a593Smuzhiyun 		return PTR_ERR(page);
159*4882a593Smuzhiyun 	if (count != cnt)
160*4882a593Smuzhiyun 		pos = __ntfs_bitmap_set_bits_in_run(vi, start_bit, count - cnt,
161*4882a593Smuzhiyun 				value ? 0 : 1, true);
162*4882a593Smuzhiyun 	else
163*4882a593Smuzhiyun 		pos = 0;
164*4882a593Smuzhiyun 	if (!pos) {
165*4882a593Smuzhiyun 		/* Rollback was successful. */
166*4882a593Smuzhiyun 		ntfs_error(vi->i_sb, "Failed to map subsequent page (error "
167*4882a593Smuzhiyun 				"%li), aborting.", PTR_ERR(page));
168*4882a593Smuzhiyun 	} else {
169*4882a593Smuzhiyun 		/* Rollback failed. */
170*4882a593Smuzhiyun 		ntfs_error(vi->i_sb, "Failed to map subsequent page (error "
171*4882a593Smuzhiyun 				"%li) and rollback failed (error %i).  "
172*4882a593Smuzhiyun 				"Aborting and leaving inconsistent metadata.  "
173*4882a593Smuzhiyun 				"Unmount and run chkdsk.", PTR_ERR(page), pos);
174*4882a593Smuzhiyun 		NVolSetErrors(NTFS_SB(vi->i_sb));
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 	return PTR_ERR(page);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun #endif /* NTFS_RW */
180