Skip to content

Commit 25307ca

Browse files
joannekoongMiklos Szeredi
authored andcommitted
fuse: simplify logic in fuse_notify_store() and fuse_retrieve()
Simplify the folio parsing logic in fuse_notify_store() and fuse_retrieve(). In particular, calculate the index by tracking pos, which allows us to remove calculating nr_pages, and use "pos" in place of outarg's offset field. Suggested-by: Jingbo Xu <jefflexu@linux.alibaba.com> Signed-off-by: Joanne Koong <joannelkoong@gmail.com> Reviewed-by: Jingbo Xu <jefflexu@linux.alibaba.com> Reviewed-by: Darrick J. Wong <djwong@kernel.org> Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
1 parent 6516147 commit 25307ca

1 file changed

Lines changed: 16 additions & 24 deletions

File tree

fs/fuse/dev.c

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,10 +1767,9 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
17671767
struct address_space *mapping;
17681768
u64 nodeid;
17691769
int err;
1770-
pgoff_t index;
1771-
unsigned int offset;
17721770
unsigned int num;
17731771
loff_t file_size;
1772+
loff_t pos;
17741773
loff_t end;
17751774

17761775
if (size < sizeof(outarg))
@@ -1787,7 +1786,8 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
17871786
return -EINVAL;
17881787

17891788
nodeid = outarg.nodeid;
1790-
num = min(outarg.size, MAX_LFS_FILESIZE - outarg.offset);
1789+
pos = outarg.offset;
1790+
num = min(outarg.size, MAX_LFS_FILESIZE - pos);
17911791

17921792
down_read(&fc->killsb);
17931793

@@ -1797,10 +1797,8 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
17971797
goto out_up_killsb;
17981798

17991799
mapping = inode->i_mapping;
1800-
index = outarg.offset >> PAGE_SHIFT;
1801-
offset = outarg.offset & ~PAGE_MASK;
18021800
file_size = i_size_read(inode);
1803-
end = outarg.offset + num;
1801+
end = pos + num;
18041802
if (end > file_size) {
18051803
file_size = end;
18061804
fuse_write_update_attr(inode, file_size, num);
@@ -1810,19 +1808,18 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
18101808
struct folio *folio;
18111809
unsigned int folio_offset;
18121810
unsigned int nr_bytes;
1813-
unsigned int nr_pages;
1811+
pgoff_t index = pos >> PAGE_SHIFT;
18141812

18151813
folio = filemap_grab_folio(mapping, index);
18161814
err = PTR_ERR(folio);
18171815
if (IS_ERR(folio))
18181816
goto out_iput;
18191817

1820-
folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
1818+
folio_offset = offset_in_folio(folio, pos);
18211819
nr_bytes = min(num, folio_size(folio) - folio_offset);
1822-
nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
18231820

18241821
err = fuse_copy_folio(cs, &folio, folio_offset, nr_bytes, 0);
1825-
if (!folio_test_uptodate(folio) && !err && offset == 0 &&
1822+
if (!folio_test_uptodate(folio) && !err && folio_offset == 0 &&
18261823
(nr_bytes == folio_size(folio) || file_size == end)) {
18271824
folio_zero_segment(folio, nr_bytes, folio_size(folio));
18281825
folio_mark_uptodate(folio);
@@ -1833,9 +1830,8 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
18331830
if (err)
18341831
goto out_iput;
18351832

1833+
pos += nr_bytes;
18361834
num -= nr_bytes;
1837-
offset = 0;
1838-
index += nr_pages;
18391835
}
18401836

18411837
err = 0;
@@ -1867,7 +1863,6 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
18671863
{
18681864
int err;
18691865
struct address_space *mapping = inode->i_mapping;
1870-
pgoff_t index;
18711866
loff_t file_size;
18721867
unsigned int num;
18731868
unsigned int offset;
@@ -1878,15 +1873,16 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
18781873
size_t args_size = sizeof(*ra);
18791874
struct fuse_args_pages *ap;
18801875
struct fuse_args *args;
1876+
loff_t pos = outarg->offset;
18811877

1882-
offset = outarg->offset & ~PAGE_MASK;
1878+
offset = offset_in_page(pos);
18831879
file_size = i_size_read(inode);
18841880

18851881
num = min(outarg->size, fc->max_write);
1886-
if (outarg->offset > file_size)
1882+
if (pos > file_size)
18871883
num = 0;
1888-
else if (num > file_size - outarg->offset)
1889-
num = file_size - outarg->offset;
1884+
else if (num > file_size - pos)
1885+
num = file_size - pos;
18901886

18911887
num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
18921888
num_pages = min(num_pages, fc->max_pages);
@@ -1909,31 +1905,27 @@ static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
19091905
args->in_pages = true;
19101906
args->end = fuse_retrieve_end;
19111907

1912-
index = outarg->offset >> PAGE_SHIFT;
1913-
19141908
while (num && ap->num_folios < num_pages) {
19151909
struct folio *folio;
19161910
unsigned int folio_offset;
19171911
unsigned int nr_bytes;
1918-
unsigned int nr_pages;
1912+
pgoff_t index = pos >> PAGE_SHIFT;
19191913

19201914
folio = filemap_get_folio(mapping, index);
19211915
if (IS_ERR(folio))
19221916
break;
19231917

1924-
folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
1918+
folio_offset = offset_in_folio(folio, pos);
19251919
nr_bytes = min(folio_size(folio) - folio_offset, num);
1926-
nr_pages = (offset + nr_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
19271920

19281921
ap->folios[ap->num_folios] = folio;
19291922
ap->descs[ap->num_folios].offset = folio_offset;
19301923
ap->descs[ap->num_folios].length = nr_bytes;
19311924
ap->num_folios++;
19321925

1933-
offset = 0;
1926+
pos += nr_bytes;
19341927
num -= nr_bytes;
19351928
total_len += nr_bytes;
1936-
index += nr_pages;
19371929
}
19381930
ra->inarg.offset = outarg->offset;
19391931
ra->inarg.size = total_len;

0 commit comments

Comments
 (0)