Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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