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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2007 Oracle.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) static bool check_setget_bounds(const struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 				const void *ptr, unsigned off, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 	const unsigned long member_offset = (unsigned long)ptr + off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	if (member_offset > eb->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 		btrfs_warn(eb->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	"bad eb member start: ptr 0x%lx start %llu member offset %lu size %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 			(unsigned long)ptr, eb->start, member_offset, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	if (member_offset + size > eb->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		btrfs_warn(eb->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	"bad eb member end: ptr 0x%lx start %llu member offset %lu size %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 			(unsigned long)ptr, eb->start, member_offset, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * Macro templates that define helpers to read/write extent buffer data of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * given size, that are also used via ctree.h for access to item members by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * specialized helpers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * Generic helpers:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * - btrfs_set_8 (for 8/16/32/64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * - btrfs_get_8 (for 8/16/32/64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * Generic helpers with a token (cached address of the most recently accessed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * page):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * - btrfs_set_token_8 (for 8/16/32/64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * - btrfs_get_token_8 (for 8/16/32/64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * The set/get functions handle data spanning two pages transparently, in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * metadata block size is larger than page.  Every pointer to metadata items is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * an offset into the extent buffer page array, cast to a specific type.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * gives us all the type checking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * The extent buffer pages stored in the array pages do not form a contiguous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * phyusical range, but the API functions assume the linear offset to the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * from 0 to metadata node size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define DEFINE_BTRFS_SETGET_BITS(bits)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) u##bits btrfs_get_token_##bits(struct btrfs_map_token *token,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			       const void *ptr, unsigned long off)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	const unsigned long member_offset = (unsigned long)ptr + off;	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	const unsigned long idx = member_offset >> PAGE_SHIFT;		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	const unsigned long oip = offset_in_page(member_offset);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	const int size = sizeof(u##bits);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	u8 lebytes[sizeof(u##bits)];					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	const int part = PAGE_SIZE - oip;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	ASSERT(token);							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	ASSERT(token->kaddr);						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	ASSERT(check_setget_bounds(token->eb, ptr, off, size));		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (token->offset <= member_offset &&				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	    member_offset + size <= token->offset + PAGE_SIZE) {	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		return get_unaligned_le##bits(token->kaddr + oip);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	token->kaddr = page_address(token->eb->pages[idx]);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	token->offset = idx << PAGE_SHIFT;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (oip + size <= PAGE_SIZE)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		return get_unaligned_le##bits(token->kaddr + oip);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	memcpy(lebytes, token->kaddr + oip, part);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	token->kaddr = page_address(token->eb->pages[idx + 1]);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	token->offset = (idx + 1) << PAGE_SHIFT;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	memcpy(lebytes + part, token->kaddr, size - part);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return get_unaligned_le##bits(lebytes);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) u##bits btrfs_get_##bits(const struct extent_buffer *eb,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			 const void *ptr, unsigned long off)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	const unsigned long member_offset = (unsigned long)ptr + off;	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	const unsigned long oip = offset_in_page(member_offset);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	const unsigned long idx = member_offset >> PAGE_SHIFT;		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	char *kaddr = page_address(eb->pages[idx]);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	const int size = sizeof(u##bits);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	const int part = PAGE_SIZE - oip;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	u8 lebytes[sizeof(u##bits)];					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	ASSERT(check_setget_bounds(eb, ptr, off, size));		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (oip + size <= PAGE_SIZE)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return get_unaligned_le##bits(kaddr + oip);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	memcpy(lebytes, kaddr + oip, part);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	kaddr = page_address(eb->pages[idx + 1]);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	memcpy(lebytes + part, kaddr, size - part);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	return get_unaligned_le##bits(lebytes);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) void btrfs_set_token_##bits(struct btrfs_map_token *token,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			    const void *ptr, unsigned long off,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			    u##bits val)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	const unsigned long member_offset = (unsigned long)ptr + off;	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	const unsigned long idx = member_offset >> PAGE_SHIFT;		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	const unsigned long oip = offset_in_page(member_offset);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	const int size = sizeof(u##bits);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	u8 lebytes[sizeof(u##bits)];					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	const int part = PAGE_SIZE - oip;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	ASSERT(token);							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	ASSERT(token->kaddr);						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	ASSERT(check_setget_bounds(token->eb, ptr, off, size));		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (token->offset <= member_offset &&				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	    member_offset + size <= token->offset + PAGE_SIZE) {	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		put_unaligned_le##bits(val, token->kaddr + oip);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	}								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	token->kaddr = page_address(token->eb->pages[idx]);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	token->offset = idx << PAGE_SHIFT;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (oip + size <= PAGE_SIZE) {					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		put_unaligned_le##bits(val, token->kaddr + oip);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		return;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	put_unaligned_le##bits(val, lebytes);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	memcpy(token->kaddr + oip, lebytes, part);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	token->kaddr = page_address(token->eb->pages[idx + 1]);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	token->offset = (idx + 1) << PAGE_SHIFT;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	memcpy(token->kaddr, lebytes + part, size - part);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		      unsigned long off, u##bits val)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	const unsigned long member_offset = (unsigned long)ptr + off;	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	const unsigned long oip = offset_in_page(member_offset);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	const unsigned long idx = member_offset >> PAGE_SHIFT;		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	char *kaddr = page_address(eb->pages[idx]);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	const int size = sizeof(u##bits);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	const int part = PAGE_SIZE - oip;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	u8 lebytes[sizeof(u##bits)];					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	ASSERT(check_setget_bounds(eb, ptr, off, size));		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (oip + size <= PAGE_SIZE) {					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		put_unaligned_le##bits(val, kaddr + oip);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	}								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	put_unaligned_le##bits(val, lebytes);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	memcpy(kaddr + oip, lebytes, part);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	kaddr = page_address(eb->pages[idx + 1]);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	memcpy(kaddr, lebytes + part, size - part);			\
^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) DEFINE_BTRFS_SETGET_BITS(8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) DEFINE_BTRFS_SETGET_BITS(16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) DEFINE_BTRFS_SETGET_BITS(32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) DEFINE_BTRFS_SETGET_BITS(64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) void btrfs_node_key(const struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		    struct btrfs_disk_key *disk_key, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	unsigned long ptr = btrfs_node_key_ptr_offset(nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		       struct btrfs_key_ptr, key, disk_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }