Commit 7073017a authored by Johannes Thumshirn's avatar Johannes Thumshirn Committed by David Sterba

btrfs: use offset_in_page instead of open-coding it

Constructs like 'var & (PAGE_SIZE - 1)' or 'var & ~PAGE_MASK' can denote an
offset into a page.

So replace them by the offset_in_page() macro instead of open-coding it if
they're not used as an alignment check.
Reviewed-by: default avatarNikolay Borisov <nborisov@suse.com>
Signed-off-by: default avatarJohannes Thumshirn <jthumshirn@suse.de>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent cb5583dd
...@@ -1202,24 +1202,24 @@ static void btrfsic_read_from_block_data( ...@@ -1202,24 +1202,24 @@ static void btrfsic_read_from_block_data(
void *dstv, u32 offset, size_t len) void *dstv, u32 offset, size_t len)
{ {
size_t cur; size_t cur;
size_t offset_in_page; size_t pgoff;
char *kaddr; char *kaddr;
char *dst = (char *)dstv; char *dst = (char *)dstv;
size_t start_offset = block_ctx->start & ((u64)PAGE_SIZE - 1); size_t start_offset = offset_in_page(block_ctx->start);
unsigned long i = (start_offset + offset) >> PAGE_SHIFT; unsigned long i = (start_offset + offset) >> PAGE_SHIFT;
WARN_ON(offset + len > block_ctx->len); WARN_ON(offset + len > block_ctx->len);
offset_in_page = (start_offset + offset) & (PAGE_SIZE - 1); pgoff = offset_in_page(start_offset + offset);
while (len > 0) { while (len > 0) {
cur = min(len, ((size_t)PAGE_SIZE - offset_in_page)); cur = min(len, ((size_t)PAGE_SIZE - pgoff));
BUG_ON(i >= DIV_ROUND_UP(block_ctx->len, PAGE_SIZE)); BUG_ON(i >= DIV_ROUND_UP(block_ctx->len, PAGE_SIZE));
kaddr = block_ctx->datav[i]; kaddr = block_ctx->datav[i];
memcpy(dst, kaddr + offset_in_page, cur); memcpy(dst, kaddr + pgoff, cur);
dst += cur; dst += cur;
len -= cur; len -= cur;
offset_in_page = 0; pgoff = 0;
i++; i++;
} }
} }
......
...@@ -477,7 +477,7 @@ static noinline int add_ra_bio_pages(struct inode *inode, ...@@ -477,7 +477,7 @@ static noinline int add_ra_bio_pages(struct inode *inode,
if (page->index == end_index) { if (page->index == end_index) {
char *userpage; char *userpage;
size_t zero_offset = isize & (PAGE_SIZE - 1); size_t zero_offset = offset_in_page(isize);
if (zero_offset) { if (zero_offset) {
int zeros; int zeros;
......
...@@ -2585,7 +2585,7 @@ static void end_bio_extent_readpage(struct bio *bio) ...@@ -2585,7 +2585,7 @@ static void end_bio_extent_readpage(struct bio *bio)
unsigned off; unsigned off;
/* Zero out the end if this page straddles i_size */ /* Zero out the end if this page straddles i_size */
off = i_size & (PAGE_SIZE-1); off = offset_in_page(i_size);
if (page->index == end_index && off) if (page->index == end_index && off)
zero_user_segment(page, off, PAGE_SIZE); zero_user_segment(page, off, PAGE_SIZE);
SetPageUptodate(page); SetPageUptodate(page);
...@@ -2888,7 +2888,7 @@ static int __do_readpage(struct extent_io_tree *tree, ...@@ -2888,7 +2888,7 @@ static int __do_readpage(struct extent_io_tree *tree,
if (page->index == last_byte >> PAGE_SHIFT) { if (page->index == last_byte >> PAGE_SHIFT) {
char *userpage; char *userpage;
size_t zero_offset = last_byte & (PAGE_SIZE - 1); size_t zero_offset = offset_in_page(last_byte);
if (zero_offset) { if (zero_offset) {
iosize = PAGE_SIZE - zero_offset; iosize = PAGE_SIZE - zero_offset;
...@@ -3432,7 +3432,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc, ...@@ -3432,7 +3432,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
ClearPageError(page); ClearPageError(page);
pg_offset = i_size & (PAGE_SIZE - 1); pg_offset = offset_in_page(i_size);
if (page->index > end_index || if (page->index > end_index ||
(page->index == end_index && !pg_offset)) { (page->index == end_index && !pg_offset)) {
page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
...@@ -5307,7 +5307,7 @@ void read_extent_buffer(const struct extent_buffer *eb, void *dstv, ...@@ -5307,7 +5307,7 @@ void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
struct page *page; struct page *page;
char *kaddr; char *kaddr;
char *dst = (char *)dstv; char *dst = (char *)dstv;
size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); size_t start_offset = offset_in_page(eb->start);
unsigned long i = (start_offset + start) >> PAGE_SHIFT; unsigned long i = (start_offset + start) >> PAGE_SHIFT;
if (start + len > eb->len) { if (start + len > eb->len) {
...@@ -5317,7 +5317,7 @@ void read_extent_buffer(const struct extent_buffer *eb, void *dstv, ...@@ -5317,7 +5317,7 @@ void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
return; return;
} }
offset = (start_offset + start) & (PAGE_SIZE - 1); offset = offset_in_page(start_offset + start);
while (len > 0) { while (len > 0) {
page = eb->pages[i]; page = eb->pages[i];
...@@ -5342,14 +5342,14 @@ int read_extent_buffer_to_user(const struct extent_buffer *eb, ...@@ -5342,14 +5342,14 @@ int read_extent_buffer_to_user(const struct extent_buffer *eb,
struct page *page; struct page *page;
char *kaddr; char *kaddr;
char __user *dst = (char __user *)dstv; char __user *dst = (char __user *)dstv;
size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); size_t start_offset = offset_in_page(eb->start);
unsigned long i = (start_offset + start) >> PAGE_SHIFT; unsigned long i = (start_offset + start) >> PAGE_SHIFT;
int ret = 0; int ret = 0;
WARN_ON(start > eb->len); WARN_ON(start > eb->len);
WARN_ON(start + len > eb->start + eb->len); WARN_ON(start + len > eb->start + eb->len);
offset = (start_offset + start) & (PAGE_SIZE - 1); offset = offset_in_page(start_offset + start);
while (len > 0) { while (len > 0) {
page = eb->pages[i]; page = eb->pages[i];
...@@ -5383,7 +5383,7 @@ int map_private_extent_buffer(const struct extent_buffer *eb, ...@@ -5383,7 +5383,7 @@ int map_private_extent_buffer(const struct extent_buffer *eb,
size_t offset; size_t offset;
char *kaddr; char *kaddr;
struct page *p; struct page *p;
size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); size_t start_offset = offset_in_page(eb->start);
unsigned long i = (start_offset + start) >> PAGE_SHIFT; unsigned long i = (start_offset + start) >> PAGE_SHIFT;
unsigned long end_i = (start_offset + start + min_len - 1) >> unsigned long end_i = (start_offset + start + min_len - 1) >>
PAGE_SHIFT; PAGE_SHIFT;
...@@ -5420,14 +5420,14 @@ int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, ...@@ -5420,14 +5420,14 @@ int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
struct page *page; struct page *page;
char *kaddr; char *kaddr;
char *ptr = (char *)ptrv; char *ptr = (char *)ptrv;
size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); size_t start_offset = offset_in_page(eb->start);
unsigned long i = (start_offset + start) >> PAGE_SHIFT; unsigned long i = (start_offset + start) >> PAGE_SHIFT;
int ret = 0; int ret = 0;
WARN_ON(start > eb->len); WARN_ON(start > eb->len);
WARN_ON(start + len > eb->start + eb->len); WARN_ON(start + len > eb->start + eb->len);
offset = (start_offset + start) & (PAGE_SIZE - 1); offset = offset_in_page(start_offset + start);
while (len > 0) { while (len > 0) {
page = eb->pages[i]; page = eb->pages[i];
...@@ -5476,13 +5476,13 @@ void write_extent_buffer(struct extent_buffer *eb, const void *srcv, ...@@ -5476,13 +5476,13 @@ void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
struct page *page; struct page *page;
char *kaddr; char *kaddr;
char *src = (char *)srcv; char *src = (char *)srcv;
size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); size_t start_offset = offset_in_page(eb->start);
unsigned long i = (start_offset + start) >> PAGE_SHIFT; unsigned long i = (start_offset + start) >> PAGE_SHIFT;
WARN_ON(start > eb->len); WARN_ON(start > eb->len);
WARN_ON(start + len > eb->start + eb->len); WARN_ON(start + len > eb->start + eb->len);
offset = (start_offset + start) & (PAGE_SIZE - 1); offset = offset_in_page(start_offset + start);
while (len > 0) { while (len > 0) {
page = eb->pages[i]; page = eb->pages[i];
...@@ -5506,13 +5506,13 @@ void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start, ...@@ -5506,13 +5506,13 @@ void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
size_t offset; size_t offset;
struct page *page; struct page *page;
char *kaddr; char *kaddr;
size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); size_t start_offset = offset_in_page(eb->start);
unsigned long i = (start_offset + start) >> PAGE_SHIFT; unsigned long i = (start_offset + start) >> PAGE_SHIFT;
WARN_ON(start > eb->len); WARN_ON(start > eb->len);
WARN_ON(start + len > eb->start + eb->len); WARN_ON(start + len > eb->start + eb->len);
offset = (start_offset + start) & (PAGE_SIZE - 1); offset = offset_in_page(start_offset + start);
while (len > 0) { while (len > 0) {
page = eb->pages[i]; page = eb->pages[i];
...@@ -5551,13 +5551,12 @@ void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, ...@@ -5551,13 +5551,12 @@ void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
size_t offset; size_t offset;
struct page *page; struct page *page;
char *kaddr; char *kaddr;
size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); size_t start_offset = offset_in_page(dst->start);
unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT; unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
WARN_ON(src->len != dst_len); WARN_ON(src->len != dst_len);
offset = (start_offset + dst_offset) & offset = offset_in_page(start_offset + dst_offset);
(PAGE_SIZE - 1);
while (len > 0) { while (len > 0) {
page = dst->pages[i]; page = dst->pages[i];
...@@ -5593,7 +5592,7 @@ static inline void eb_bitmap_offset(struct extent_buffer *eb, ...@@ -5593,7 +5592,7 @@ static inline void eb_bitmap_offset(struct extent_buffer *eb,
unsigned long *page_index, unsigned long *page_index,
size_t *page_offset) size_t *page_offset)
{ {
size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1); size_t start_offset = offset_in_page(eb->start);
size_t byte_offset = BIT_BYTE(nr); size_t byte_offset = BIT_BYTE(nr);
size_t offset; size_t offset;
...@@ -5605,7 +5604,7 @@ static inline void eb_bitmap_offset(struct extent_buffer *eb, ...@@ -5605,7 +5604,7 @@ static inline void eb_bitmap_offset(struct extent_buffer *eb,
offset = start_offset + start + byte_offset; offset = start_offset + start + byte_offset;
*page_index = offset >> PAGE_SHIFT; *page_index = offset >> PAGE_SHIFT;
*page_offset = offset & (PAGE_SIZE - 1); *page_offset = offset_in_page(offset);
} }
/** /**
...@@ -5747,7 +5746,7 @@ void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, ...@@ -5747,7 +5746,7 @@ void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
size_t cur; size_t cur;
size_t dst_off_in_page; size_t dst_off_in_page;
size_t src_off_in_page; size_t src_off_in_page;
size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); size_t start_offset = offset_in_page(dst->start);
unsigned long dst_i; unsigned long dst_i;
unsigned long src_i; unsigned long src_i;
...@@ -5765,10 +5764,8 @@ void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, ...@@ -5765,10 +5764,8 @@ void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
} }
while (len > 0) { while (len > 0) {
dst_off_in_page = (start_offset + dst_offset) & dst_off_in_page = offset_in_page(start_offset + dst_offset);
(PAGE_SIZE - 1); src_off_in_page = offset_in_page(start_offset + src_offset);
src_off_in_page = (start_offset + src_offset) &
(PAGE_SIZE - 1);
dst_i = (start_offset + dst_offset) >> PAGE_SHIFT; dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
src_i = (start_offset + src_offset) >> PAGE_SHIFT; src_i = (start_offset + src_offset) >> PAGE_SHIFT;
...@@ -5796,7 +5793,7 @@ void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, ...@@ -5796,7 +5793,7 @@ void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
size_t src_off_in_page; size_t src_off_in_page;
unsigned long dst_end = dst_offset + len - 1; unsigned long dst_end = dst_offset + len - 1;
unsigned long src_end = src_offset + len - 1; unsigned long src_end = src_offset + len - 1;
size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1); size_t start_offset = offset_in_page(dst->start);
unsigned long dst_i; unsigned long dst_i;
unsigned long src_i; unsigned long src_i;
...@@ -5820,10 +5817,8 @@ void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, ...@@ -5820,10 +5817,8 @@ void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
dst_i = (start_offset + dst_end) >> PAGE_SHIFT; dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
src_i = (start_offset + src_end) >> PAGE_SHIFT; src_i = (start_offset + src_end) >> PAGE_SHIFT;
dst_off_in_page = (start_offset + dst_end) & dst_off_in_page = offset_in_page(start_offset + dst_end);
(PAGE_SIZE - 1); src_off_in_page = offset_in_page(start_offset + src_end);
src_off_in_page = (start_offset + src_end) &
(PAGE_SIZE - 1);
cur = min_t(unsigned long, len, src_off_in_page + 1); cur = min_t(unsigned long, len, src_off_in_page + 1);
cur = min(cur, dst_off_in_page + 1); cur = min(cur, dst_off_in_page + 1);
......
...@@ -399,7 +399,7 @@ static noinline int btrfs_copy_from_user(loff_t pos, size_t write_bytes, ...@@ -399,7 +399,7 @@ static noinline int btrfs_copy_from_user(loff_t pos, size_t write_bytes,
size_t copied = 0; size_t copied = 0;
size_t total_copied = 0; size_t total_copied = 0;
int pg = 0; int pg = 0;
int offset = pos & (PAGE_SIZE - 1); int offset = offset_in_page(pos);
while (write_bytes > 0) { while (write_bytes > 0) {
size_t count = min_t(size_t, size_t count = min_t(size_t,
...@@ -1611,7 +1611,7 @@ static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb, ...@@ -1611,7 +1611,7 @@ static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,
return -ENOMEM; return -ENOMEM;
while (iov_iter_count(i) > 0) { while (iov_iter_count(i) > 0) {
size_t offset = pos & (PAGE_SIZE - 1); size_t offset = offset_in_page(pos);
size_t sector_offset; size_t sector_offset;
size_t write_bytes = min(iov_iter_count(i), size_t write_bytes = min(iov_iter_count(i),
nrptrs * (size_t)PAGE_SIZE - nrptrs * (size_t)PAGE_SIZE -
......
...@@ -240,7 +240,7 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans, ...@@ -240,7 +240,7 @@ static int insert_inline_extent(struct btrfs_trans_handle *trans,
start >> PAGE_SHIFT); start >> PAGE_SHIFT);
btrfs_set_file_extent_compression(leaf, ei, 0); btrfs_set_file_extent_compression(leaf, ei, 0);
kaddr = kmap_atomic(page); kaddr = kmap_atomic(page);
offset = start & (PAGE_SIZE - 1); offset = offset_in_page(start);
write_extent_buffer(leaf, kaddr + offset, ptr, size); write_extent_buffer(leaf, kaddr + offset, ptr, size);
kunmap_atomic(kaddr); kunmap_atomic(kaddr);
put_page(page); put_page(page);
...@@ -549,8 +549,7 @@ static noinline void compress_file_range(struct inode *inode, ...@@ -549,8 +549,7 @@ static noinline void compress_file_range(struct inode *inode,
&total_compressed); &total_compressed);
if (!ret) { if (!ret) {
unsigned long offset = total_compressed & unsigned long offset = offset_in_page(total_compressed);
(PAGE_SIZE - 1);
struct page *page = pages[nr_pages - 1]; struct page *page = pages[nr_pages - 1];
char *kaddr; char *kaddr;
...@@ -8906,7 +8905,7 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) ...@@ -8906,7 +8905,7 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
/* page is wholly or partially inside EOF */ /* page is wholly or partially inside EOF */
if (page_start + PAGE_SIZE > size) if (page_start + PAGE_SIZE > size)
zero_start = size & ~PAGE_MASK; zero_start = offset_in_page(size);
else else
zero_start = PAGE_SIZE; zero_start = PAGE_SIZE;
......
...@@ -4775,7 +4775,7 @@ static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len) ...@@ -4775,7 +4775,7 @@ static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
struct btrfs_key key; struct btrfs_key key;
pgoff_t index = offset >> PAGE_SHIFT; pgoff_t index = offset >> PAGE_SHIFT;
pgoff_t last_index; pgoff_t last_index;
unsigned pg_offset = offset & ~PAGE_MASK; unsigned pg_offset = offset_in_page(offset);
ssize_t ret = 0; ssize_t ret = 0;
key.objectid = sctx->cur_ino; key.objectid = sctx->cur_ino;
......
...@@ -1436,7 +1436,7 @@ static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr, ...@@ -1436,7 +1436,7 @@ static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
p = kmap(*page); p = kmap(*page);
/* align our pointer to the offset of the super block */ /* align our pointer to the offset of the super block */
*disk_super = p + (bytenr & ~PAGE_MASK); *disk_super = p + offset_in_page(bytenr);
if (btrfs_super_bytenr(*disk_super) != bytenr || if (btrfs_super_bytenr(*disk_super) != bytenr ||
btrfs_super_magic(*disk_super) != BTRFS_MAGIC) { btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment