Skip to content

Commit 02e83f4

Browse files
committed
vfs: move generic_remap_checks out of mm
I would like to move all the generic helpers for the vfs remap range functionality (aka clonerange and dedupe) into a separate file so that they won't be scattered across the vfs and the mm subsystems. The eventual goal is to be able to deselect remap_range.c if none of the filesystems need that code, but the tricky part here is picking a stable(ish) part of the merge window to rearrange code. Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
1 parent bbf5c97 commit 02e83f4

4 files changed

Lines changed: 102 additions & 81 deletions

File tree

fs/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ obj-y := open.o read_write.o file_table.o super.o \
1313
seq_file.o xattr.o libfs.o fs-writeback.o \
1414
pnode.o splice.o sync.o utimes.o d_path.o \
1515
stack.o fs_struct.o statfs.o fs_pin.o nsfs.o \
16-
fs_types.o fs_context.o fs_parser.o fsopen.o init.o
16+
fs_types.o fs_context.o fs_parser.o fsopen.o init.o \
17+
remap_range.o
1718

1819
ifeq ($(CONFIG_BLOCK),y)
1920
obj-y += buffer.o block_dev.o direct-io.o mpage.o

fs/remap_range.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include <linux/slab.h>
3+
#include <linux/stat.h>
4+
#include <linux/sched/xacct.h>
5+
#include <linux/fcntl.h>
6+
#include <linux/file.h>
7+
#include <linux/uio.h>
8+
#include <linux/fsnotify.h>
9+
#include <linux/security.h>
10+
#include <linux/export.h>
11+
#include <linux/syscalls.h>
12+
#include <linux/pagemap.h>
13+
#include <linux/splice.h>
14+
#include <linux/compat.h>
15+
#include <linux/mount.h>
16+
#include <linux/fs.h>
17+
#include "internal.h"
18+
19+
#include <linux/uaccess.h>
20+
#include <asm/unistd.h>
21+
22+
/*
23+
* Performs necessary checks before doing a clone.
24+
*
25+
* Can adjust amount of bytes to clone via @req_count argument.
26+
* Returns appropriate error code that caller should return or
27+
* zero in case the clone should be allowed.
28+
*/
29+
int generic_remap_checks(struct file *file_in, loff_t pos_in,
30+
struct file *file_out, loff_t pos_out,
31+
loff_t *req_count, unsigned int remap_flags)
32+
{
33+
struct inode *inode_in = file_in->f_mapping->host;
34+
struct inode *inode_out = file_out->f_mapping->host;
35+
uint64_t count = *req_count;
36+
uint64_t bcount;
37+
loff_t size_in, size_out;
38+
loff_t bs = inode_out->i_sb->s_blocksize;
39+
int ret;
40+
41+
/* The start of both ranges must be aligned to an fs block. */
42+
if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs))
43+
return -EINVAL;
44+
45+
/* Ensure offsets don't wrap. */
46+
if (pos_in + count < pos_in || pos_out + count < pos_out)
47+
return -EINVAL;
48+
49+
size_in = i_size_read(inode_in);
50+
size_out = i_size_read(inode_out);
51+
52+
/* Dedupe requires both ranges to be within EOF. */
53+
if ((remap_flags & REMAP_FILE_DEDUP) &&
54+
(pos_in >= size_in || pos_in + count > size_in ||
55+
pos_out >= size_out || pos_out + count > size_out))
56+
return -EINVAL;
57+
58+
/* Ensure the infile range is within the infile. */
59+
if (pos_in >= size_in)
60+
return -EINVAL;
61+
count = min(count, size_in - (uint64_t)pos_in);
62+
63+
ret = generic_write_check_limits(file_out, pos_out, &count);
64+
if (ret)
65+
return ret;
66+
67+
/*
68+
* If the user wanted us to link to the infile's EOF, round up to the
69+
* next block boundary for this check.
70+
*
71+
* Otherwise, make sure the count is also block-aligned, having
72+
* already confirmed the starting offsets' block alignment.
73+
*/
74+
if (pos_in + count == size_in) {
75+
bcount = ALIGN(size_in, bs) - pos_in;
76+
} else {
77+
if (!IS_ALIGNED(count, bs))
78+
count = ALIGN_DOWN(count, bs);
79+
bcount = count;
80+
}
81+
82+
/* Don't allow overlapped cloning within the same file. */
83+
if (inode_in == inode_out &&
84+
pos_out + bcount > pos_in &&
85+
pos_out < pos_in + bcount)
86+
return -EINVAL;
87+
88+
/*
89+
* We shortened the request but the caller can't deal with that, so
90+
* bounce the request back to userspace.
91+
*/
92+
if (*req_count != count && !(remap_flags & REMAP_FILE_CAN_SHORTEN))
93+
return -EINVAL;
94+
95+
*req_count = count;
96+
return 0;
97+
}

include/linux/fs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3012,6 +3012,8 @@ extern ssize_t generic_write_checks(struct kiocb *, struct iov_iter *);
30123012
extern int generic_remap_checks(struct file *file_in, loff_t pos_in,
30133013
struct file *file_out, loff_t pos_out,
30143014
loff_t *count, unsigned int remap_flags);
3015+
extern int generic_write_check_limits(struct file *file, loff_t pos,
3016+
loff_t *count);
30153017
extern int generic_file_rw_checks(struct file *file_in, struct file *file_out);
30163018
extern int generic_copy_file_checks(struct file *file_in, loff_t pos_in,
30173019
struct file *file_out, loff_t pos_out,

mm/filemap.c

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,8 +3098,7 @@ EXPORT_SYMBOL(read_cache_page_gfp);
30983098
* LFS limits. If pos is under the limit it becomes a short access. If it
30993099
* exceeds the limit we return -EFBIG.
31003100
*/
3101-
static int generic_write_check_limits(struct file *file, loff_t pos,
3102-
loff_t *count)
3101+
int generic_write_check_limits(struct file *file, loff_t pos, loff_t *count)
31033102
{
31043103
struct inode *inode = file->f_mapping->host;
31053104
loff_t max_size = inode->i_sb->s_maxbytes;
@@ -3161,84 +3160,6 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
31613160
}
31623161
EXPORT_SYMBOL(generic_write_checks);
31633162

3164-
/*
3165-
* Performs necessary checks before doing a clone.
3166-
*
3167-
* Can adjust amount of bytes to clone via @req_count argument.
3168-
* Returns appropriate error code that caller should return or
3169-
* zero in case the clone should be allowed.
3170-
*/
3171-
int generic_remap_checks(struct file *file_in, loff_t pos_in,
3172-
struct file *file_out, loff_t pos_out,
3173-
loff_t *req_count, unsigned int remap_flags)
3174-
{
3175-
struct inode *inode_in = file_in->f_mapping->host;
3176-
struct inode *inode_out = file_out->f_mapping->host;
3177-
uint64_t count = *req_count;
3178-
uint64_t bcount;
3179-
loff_t size_in, size_out;
3180-
loff_t bs = inode_out->i_sb->s_blocksize;
3181-
int ret;
3182-
3183-
/* The start of both ranges must be aligned to an fs block. */
3184-
if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs))
3185-
return -EINVAL;
3186-
3187-
/* Ensure offsets don't wrap. */
3188-
if (pos_in + count < pos_in || pos_out + count < pos_out)
3189-
return -EINVAL;
3190-
3191-
size_in = i_size_read(inode_in);
3192-
size_out = i_size_read(inode_out);
3193-
3194-
/* Dedupe requires both ranges to be within EOF. */
3195-
if ((remap_flags & REMAP_FILE_DEDUP) &&
3196-
(pos_in >= size_in || pos_in + count > size_in ||
3197-
pos_out >= size_out || pos_out + count > size_out))
3198-
return -EINVAL;
3199-
3200-
/* Ensure the infile range is within the infile. */
3201-
if (pos_in >= size_in)
3202-
return -EINVAL;
3203-
count = min(count, size_in - (uint64_t)pos_in);
3204-
3205-
ret = generic_write_check_limits(file_out, pos_out, &count);
3206-
if (ret)
3207-
return ret;
3208-
3209-
/*
3210-
* If the user wanted us to link to the infile's EOF, round up to the
3211-
* next block boundary for this check.
3212-
*
3213-
* Otherwise, make sure the count is also block-aligned, having
3214-
* already confirmed the starting offsets' block alignment.
3215-
*/
3216-
if (pos_in + count == size_in) {
3217-
bcount = ALIGN(size_in, bs) - pos_in;
3218-
} else {
3219-
if (!IS_ALIGNED(count, bs))
3220-
count = ALIGN_DOWN(count, bs);
3221-
bcount = count;
3222-
}
3223-
3224-
/* Don't allow overlapped cloning within the same file. */
3225-
if (inode_in == inode_out &&
3226-
pos_out + bcount > pos_in &&
3227-
pos_out < pos_in + bcount)
3228-
return -EINVAL;
3229-
3230-
/*
3231-
* We shortened the request but the caller can't deal with that, so
3232-
* bounce the request back to userspace.
3233-
*/
3234-
if (*req_count != count && !(remap_flags & REMAP_FILE_CAN_SHORTEN))
3235-
return -EINVAL;
3236-
3237-
*req_count = count;
3238-
return 0;
3239-
}
3240-
3241-
32423163
/*
32433164
* Performs common checks before doing a file copy/clone
32443165
* from @file_in to @file_out.

0 commit comments

Comments
 (0)