Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2007 Oracle.  All rights reserved.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  4 */
  5
 
  6#include <asm/unaligned.h>
  7
  8#include "ctree.h"
  9
 10static inline u8 get_unaligned_le8(const void *p)
 11{
 12       return *(u8 *)p;
 13}
 14
 15static inline void put_unaligned_le8(u8 val, void *p)
 16{
 17       *(u8 *)p = val;
 18}
 19
 20static bool check_setget_bounds(const struct extent_buffer *eb,
 21				const void *ptr, unsigned off, int size)
 22{
 23	const unsigned long member_offset = (unsigned long)ptr + off;
 24
 25	if (member_offset > eb->len) {
 26		btrfs_warn(eb->fs_info,
 27	"bad eb member start: ptr 0x%lx start %llu member offset %lu size %d",
 28			(unsigned long)ptr, eb->start, member_offset, size);
 29		return false;
 30	}
 31	if (member_offset + size > eb->len) {
 32		btrfs_warn(eb->fs_info,
 33	"bad eb member end: ptr 0x%lx start %llu member offset %lu size %d",
 34			(unsigned long)ptr, eb->start, member_offset, size);
 35		return false;
 36	}
 37
 38	return true;
 39}
 40
 41/*
 42 * Macro templates that define helpers to read/write extent buffer data of a
 43 * given size, that are also used via ctree.h for access to item members by
 44 * specialized helpers.
 45 *
 46 * Generic helpers:
 47 * - btrfs_set_8 (for 8/16/32/64)
 48 * - btrfs_get_8 (for 8/16/32/64)
 49 *
 50 * Generic helpers with a token (cached address of the most recently accessed
 51 * page):
 52 * - btrfs_set_token_8 (for 8/16/32/64)
 53 * - btrfs_get_token_8 (for 8/16/32/64)
 54 *
 55 * The set/get functions handle data spanning two pages transparently, in case
 56 * metadata block size is larger than page.  Every pointer to metadata items is
 57 * an offset into the extent buffer page array, cast to a specific type.  This
 58 * gives us all the type checking.
 59 *
 60 * The extent buffer pages stored in the array pages do not form a contiguous
 61 * phyusical range, but the API functions assume the linear offset to the range
 62 * from 0 to metadata node size.
 63 */
 64
 65#define DEFINE_BTRFS_SETGET_BITS(bits)					\
 66u##bits btrfs_get_token_##bits(struct btrfs_map_token *token,		\
 67			       const void *ptr, unsigned long off)	\
 68{									\
 69	const unsigned long member_offset = (unsigned long)ptr + off;	\
 70	const unsigned long idx = member_offset >> PAGE_SHIFT;		\
 71	const unsigned long oip = offset_in_page(member_offset);	\
 72	const int size = sizeof(u##bits);				\
 73	u8 lebytes[sizeof(u##bits)];					\
 74	const int part = PAGE_SIZE - oip;				\
 75									\
 76	ASSERT(token);							\
 77	ASSERT(token->kaddr);						\
 78	ASSERT(check_setget_bounds(token->eb, ptr, off, size));		\
 79	if (token->offset <= member_offset &&				\
 80	    member_offset + size <= token->offset + PAGE_SIZE) {	\
 81		return get_unaligned_le##bits(token->kaddr + oip);	\
 82	}								\
 83	token->kaddr = page_address(token->eb->pages[idx]);		\
 84	token->offset = idx << PAGE_SHIFT;				\
 85	if (oip + size <= PAGE_SIZE)					\
 86		return get_unaligned_le##bits(token->kaddr + oip);	\
 87									\
 88	memcpy(lebytes, token->kaddr + oip, part);			\
 89	token->kaddr = page_address(token->eb->pages[idx + 1]);		\
 90	token->offset = (idx + 1) << PAGE_SHIFT;			\
 91	memcpy(lebytes + part, token->kaddr, size - part);		\
 92	return get_unaligned_le##bits(lebytes);				\
 93}									\
 94u##bits btrfs_get_##bits(const struct extent_buffer *eb,		\
 95			 const void *ptr, unsigned long off)		\
 96{									\
 97	const unsigned long member_offset = (unsigned long)ptr + off;	\
 98	const unsigned long oip = offset_in_page(member_offset);	\
 99	const unsigned long idx = member_offset >> PAGE_SHIFT;		\
100	char *kaddr = page_address(eb->pages[idx]);			\
101	const int size = sizeof(u##bits);				\
102	const int part = PAGE_SIZE - oip;				\
103	u8 lebytes[sizeof(u##bits)];					\
104									\
105	ASSERT(check_setget_bounds(eb, ptr, off, size));		\
106	if (oip + size <= PAGE_SIZE)					\
107		return get_unaligned_le##bits(kaddr + oip);		\
108									\
109	memcpy(lebytes, kaddr + oip, part);				\
110	kaddr = page_address(eb->pages[idx + 1]);			\
111	memcpy(lebytes + part, kaddr, size - part);			\
112	return get_unaligned_le##bits(lebytes);				\
113}									\
114void btrfs_set_token_##bits(struct btrfs_map_token *token,		\
115			    const void *ptr, unsigned long off,		\
116			    u##bits val)				\
117{									\
118	const unsigned long member_offset = (unsigned long)ptr + off;	\
119	const unsigned long idx = member_offset >> PAGE_SHIFT;		\
120	const unsigned long oip = offset_in_page(member_offset);	\
121	const int size = sizeof(u##bits);				\
122	u8 lebytes[sizeof(u##bits)];					\
123	const int part = PAGE_SIZE - oip;				\
124									\
125	ASSERT(token);							\
126	ASSERT(token->kaddr);						\
127	ASSERT(check_setget_bounds(token->eb, ptr, off, size));		\
128	if (token->offset <= member_offset &&				\
129	    member_offset + size <= token->offset + PAGE_SIZE) {	\
130		put_unaligned_le##bits(val, token->kaddr + oip);	\
131		return;							\
132	}								\
133	token->kaddr = page_address(token->eb->pages[idx]);		\
134	token->offset = idx << PAGE_SHIFT;				\
135	if (oip + size <= PAGE_SIZE) {					\
136		put_unaligned_le##bits(val, token->kaddr + oip);	\
137		return;							\
 
138	}								\
139	put_unaligned_le##bits(val, lebytes);				\
140	memcpy(token->kaddr + oip, lebytes, part);			\
141	token->kaddr = page_address(token->eb->pages[idx + 1]);		\
142	token->offset = (idx + 1) << PAGE_SHIFT;			\
143	memcpy(token->kaddr, lebytes + part, size - part);		\
144}									\
145void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr,	\
146		      unsigned long off, u##bits val)			\
 
147{									\
148	const unsigned long member_offset = (unsigned long)ptr + off;	\
149	const unsigned long oip = offset_in_page(member_offset);	\
150	const unsigned long idx = member_offset >> PAGE_SHIFT;		\
151	char *kaddr = page_address(eb->pages[idx]);			\
152	const int size = sizeof(u##bits);				\
153	const int part = PAGE_SIZE - oip;				\
154	u8 lebytes[sizeof(u##bits)];					\
155									\
156	ASSERT(check_setget_bounds(eb, ptr, off, size));		\
157	if (oip + size <= PAGE_SIZE) {					\
158		put_unaligned_le##bits(val, kaddr + oip);		\
 
 
 
 
159		return;							\
160	}								\
 
 
 
 
161									\
162	put_unaligned_le##bits(val, lebytes);				\
163	memcpy(kaddr + oip, lebytes, part);				\
164	kaddr = page_address(eb->pages[idx + 1]);			\
165	memcpy(kaddr, lebytes + part, size - part);			\
 
 
 
 
 
 
 
166}
167
168DEFINE_BTRFS_SETGET_BITS(8)
169DEFINE_BTRFS_SETGET_BITS(16)
170DEFINE_BTRFS_SETGET_BITS(32)
171DEFINE_BTRFS_SETGET_BITS(64)
172
173void btrfs_node_key(const struct extent_buffer *eb,
174		    struct btrfs_disk_key *disk_key, int nr)
175{
176	unsigned long ptr = btrfs_node_key_ptr_offset(nr);
177	read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
178		       struct btrfs_key_ptr, key, disk_key);
179}
v4.10.11
 
  1/*
  2 * Copyright (C) 2007 Oracle.  All rights reserved.
  3 *
  4 * This program is free software; you can redistribute it and/or
  5 * modify it under the terms of the GNU General Public
  6 * License v2 as published by the Free Software Foundation.
  7 *
  8 * This program is distributed in the hope that it will be useful,
  9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 11 * General Public License for more details.
 12 *
 13 * You should have received a copy of the GNU General Public
 14 * License along with this program; if not, write to the
 15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 16 * Boston, MA 021110-1307, USA.
 17 */
 18
 19#include <linux/highmem.h>
 20#include <asm/unaligned.h>
 21
 22#include "ctree.h"
 23
 24static inline u8 get_unaligned_le8(const void *p)
 25{
 26       return *(u8 *)p;
 27}
 28
 29static inline void put_unaligned_le8(u8 val, void *p)
 30{
 31       *(u8 *)p = val;
 32}
 33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 34/*
 35 * this is some deeply nasty code.
 36 *
 37 * The end result is that anyone who #includes ctree.h gets a
 38 * declaration for the btrfs_set_foo functions and btrfs_foo functions,
 39 * which are wrappers of btrfs_set_token_#bits functions and
 40 * btrfs_get_token_#bits functions, which are defined in this file.
 41 *
 42 * These setget functions do all the extent_buffer related mapping
 43 * required to efficiently read and write specific fields in the extent
 44 * buffers.  Every pointer to metadata items in btrfs is really just
 45 * an unsigned long offset into the extent buffer which has been
 46 * cast to a specific type.  This gives us all the gcc type checking.
 47 *
 48 * The extent buffer api is used to do the page spanning work required to
 49 * have a metadata blocksize different from the page size.
 
 
 
 
 
 
 50 */
 51
 52#define DEFINE_BTRFS_SETGET_BITS(bits)					\
 53u##bits btrfs_get_token_##bits(struct extent_buffer *eb, void *ptr,	\
 54			       unsigned long off,			\
 55			       struct btrfs_map_token *token)		\
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 56{									\
 57	unsigned long part_offset = (unsigned long)ptr;			\
 58	unsigned long offset = part_offset + off;			\
 59	void *p;							\
 60	int err;							\
 61	char *kaddr;							\
 62	unsigned long map_start;					\
 63	unsigned long map_len;						\
 64	int size = sizeof(u##bits);					\
 65	u##bits res;							\
 66									\
 67	if (token && token->kaddr && token->offset <= offset &&		\
 68	    token->eb == eb &&						\
 69	   (token->offset + PAGE_SIZE >= offset + size)) {	\
 70		kaddr = token->kaddr;					\
 71		p = kaddr + part_offset - token->offset;		\
 72		res = get_unaligned_le##bits(p + off);			\
 73		return res;						\
 74	}								\
 75	err = map_private_extent_buffer(eb, offset, size,		\
 76					&kaddr, &map_start, &map_len);	\
 77	if (err) {							\
 78		__le##bits leres;					\
 
 
 
 
 
 79									\
 80		read_extent_buffer(eb, &leres, offset, size);		\
 81		return le##bits##_to_cpu(leres);			\
 
 
 
 
 
 82	}								\
 83	p = kaddr + part_offset - map_start;				\
 84	res = get_unaligned_le##bits(p + off);				\
 85	if (token) {							\
 86		token->kaddr = kaddr;					\
 87		token->offset = map_start;				\
 88		token->eb = eb;						\
 89	}								\
 90	return res;							\
 
 
 
 
 91}									\
 92void btrfs_set_token_##bits(struct extent_buffer *eb,			\
 93			    void *ptr, unsigned long off, u##bits val,	\
 94			    struct btrfs_map_token *token)		\
 95{									\
 96	unsigned long part_offset = (unsigned long)ptr;			\
 97	unsigned long offset = part_offset + off;			\
 98	void *p;							\
 99	int err;							\
100	char *kaddr;							\
101	unsigned long map_start;					\
102	unsigned long map_len;						\
103	int size = sizeof(u##bits);					\
104									\
105	if (token && token->kaddr && token->offset <= offset &&		\
106	    token->eb == eb &&						\
107	   (token->offset + PAGE_SIZE >= offset + size)) {	\
108		kaddr = token->kaddr;					\
109		p = kaddr + part_offset - token->offset;		\
110		put_unaligned_le##bits(val, p + off);			\
111		return;							\
112	}								\
113	err = map_private_extent_buffer(eb, offset, size,		\
114			&kaddr, &map_start, &map_len);			\
115	if (err) {							\
116		__le##bits val2;					\
117									\
118		val2 = cpu_to_le##bits(val);				\
119		write_extent_buffer(eb, &val2, offset, size);		\
120		return;							\
121	}								\
122	p = kaddr + part_offset - map_start;				\
123	put_unaligned_le##bits(val, p + off);				\
124	if (token) {							\
125		token->kaddr = kaddr;					\
126		token->offset = map_start;				\
127		token->eb = eb;						\
128	}								\
129}
130
131DEFINE_BTRFS_SETGET_BITS(8)
132DEFINE_BTRFS_SETGET_BITS(16)
133DEFINE_BTRFS_SETGET_BITS(32)
134DEFINE_BTRFS_SETGET_BITS(64)
135
136void btrfs_node_key(struct extent_buffer *eb,
137		    struct btrfs_disk_key *disk_key, int nr)
138{
139	unsigned long ptr = btrfs_node_key_ptr_offset(nr);
140	read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
141		       struct btrfs_key_ptr, key, disk_key);
142}