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)  * mst.c - NTFS multi sector transfer protection handling code. Part of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *	   Linux-NTFS project.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2001-2004 Anton Altaparmakov
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "ntfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * post_read_mst_fixup - deprotect multi sector transfer protected data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * @b:		pointer to the data to deprotect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * @size:	size in bytes of @b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * Perform the necessary post read multi sector transfer fixup and detect the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * presence of incomplete multi sector transfers. - In that case, overwrite the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * magic of the ntfs record header being processed with "BAAD" (in memory only!)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * and abort processing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * Return 0 on success and -EINVAL on error ("BAAD" magic will be present).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * NOTE: We consider the absence / invalidity of an update sequence array to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * mean that the structure is not protected at all and hence doesn't need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * be fixed up. Thus, we return success and not failure in this case. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * in contrast to pre_write_mst_fixup(), see below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) int post_read_mst_fixup(NTFS_RECORD *b, const u32 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	u16 usa_ofs, usa_count, usn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	u16 *usa_pos, *data_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	/* Setup the variables. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	usa_ofs = le16_to_cpu(b->usa_ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	/* Decrement usa_count to get number of fixups. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	usa_count = le16_to_cpu(b->usa_count) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	/* Size and alignment checks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	if ( size & (NTFS_BLOCK_SIZE - 1)	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	     usa_ofs & 1			||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	     usa_ofs + (usa_count * 2) > size	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	     (size >> NTFS_BLOCK_SIZE_BITS) != usa_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	/* Position of usn in update sequence array. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	usa_pos = (u16*)b + usa_ofs/sizeof(u16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	 * The update sequence number which has to be equal to each of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	 * u16 values before they are fixed up. Note no need to care for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	 * endianness since we are comparing and moving data for on disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	 * structures which means the data is consistent. - If it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	 * consistenty the wrong endianness it doesn't make any difference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	usn = *usa_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	 * Position in protected data of first u16 that needs fixing up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	 * Check for incomplete multi sector transfer(s).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	while (usa_count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		if (*data_pos != usn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			 * Incomplete multi sector transfer detected! )-:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			 * Set the magic to "BAAD" and return failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			 * Note that magic_BAAD is already converted to le32.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			b->magic = magic_BAAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	/* Re-setup the variables. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	usa_count = le16_to_cpu(b->usa_count) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	/* Fixup all sectors. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	while (usa_count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		 * Increment position in usa and restore original data from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		 * the usa into the data buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		*data_pos = *(++usa_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		/* Increment position in data as well. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * pre_write_mst_fixup - apply multi sector transfer protection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * @b:		pointer to the data to protect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * @size:	size in bytes of @b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * Perform the necessary pre write multi sector transfer fixup on the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  * pointer to by @b of @size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  * Return 0 if fixup applied (success) or -EINVAL if no fixup was performed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * (assumed not needed). This is in contrast to post_read_mst_fixup() above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * NOTE: We consider the absence / invalidity of an update sequence array to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  * mean that the structure is not subject to protection and hence doesn't need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * to be fixed up. This means that you have to create a valid update sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  * array header in the ntfs record before calling this function, otherwise it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * will fail (the header needs to contain the position of the update sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  * array together with the number of elements in the array). You also need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  * initialise the update sequence number before calling this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  * otherwise a random word will be used (whatever was in the record at that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  * position at that time).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int pre_write_mst_fixup(NTFS_RECORD *b, const u32 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	le16 *usa_pos, *data_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	u16 usa_ofs, usa_count, usn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	le16 le_usn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	/* Sanity check + only fixup if it makes sense. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (!b || ntfs_is_baad_record(b->magic) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			ntfs_is_hole_record(b->magic))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	/* Setup the variables. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	usa_ofs = le16_to_cpu(b->usa_ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	/* Decrement usa_count to get number of fixups. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	usa_count = le16_to_cpu(b->usa_count) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	/* Size and alignment checks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if ( size & (NTFS_BLOCK_SIZE - 1)	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	     usa_ofs & 1			||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	     usa_ofs + (usa_count * 2) > size	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	     (size >> NTFS_BLOCK_SIZE_BITS) != usa_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	/* Position of usn in update sequence array. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	usa_pos = (le16*)((u8*)b + usa_ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	 * Cyclically increment the update sequence number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	 * (skipping 0 and -1, i.e. 0xffff).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	usn = le16_to_cpup(usa_pos) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (usn == 0xffff || !usn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		usn = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	le_usn = cpu_to_le16(usn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	*usa_pos = le_usn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	/* Position in data of first u16 that needs fixing up. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	/* Fixup all sectors. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	while (usa_count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		 * Increment the position in the usa and save the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		 * original data from the data buffer into the usa.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		*(++usa_pos) = *data_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		/* Apply fixup to data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		*data_pos = le_usn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		/* Increment position in data as well. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		data_pos += NTFS_BLOCK_SIZE/sizeof(le16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * post_write_mst_fixup - fast deprotect multi sector transfer protected data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  * @b:		pointer to the data to deprotect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  * Perform the necessary post write multi sector transfer fixup, not checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * for any errors, because we assume we have just used pre_write_mst_fixup(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  * thus the data will be fine or we would never have gotten here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) void post_write_mst_fixup(NTFS_RECORD *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	le16 *usa_pos, *data_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	u16 usa_ofs = le16_to_cpu(b->usa_ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	u16 usa_count = le16_to_cpu(b->usa_count) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	/* Position of usn in update sequence array. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	usa_pos = (le16*)b + usa_ofs/sizeof(le16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	/* Position in protected data of first u16 that needs fixing up. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	/* Fixup all sectors. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	while (usa_count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		 * Increment position in usa and restore original data from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		 * the usa into the data buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		*data_pos = *(++usa_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		/* Increment position in data as well. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		data_pos += NTFS_BLOCK_SIZE/sizeof(le16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }