diff options
Diffstat (limited to 'mm/filemap.c')
-rw-r--r-- | mm/filemap.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/mm/filemap.c b/mm/filemap.c index 44361928bbb0..aac71aef4c61 100644 --- a/mm/filemap.c +++ b/mm/filemap.c | |||
@@ -3056,6 +3056,59 @@ int generic_file_rw_checks(struct file *file_in, struct file *file_out) | |||
3056 | return 0; | 3056 | return 0; |
3057 | } | 3057 | } |
3058 | 3058 | ||
3059 | /* | ||
3060 | * Performs necessary checks before doing a file copy | ||
3061 | * | ||
3062 | * Can adjust amount of bytes to copy via @req_count argument. | ||
3063 | * Returns appropriate error code that caller should return or | ||
3064 | * zero in case the copy should be allowed. | ||
3065 | */ | ||
3066 | int generic_copy_file_checks(struct file *file_in, loff_t pos_in, | ||
3067 | struct file *file_out, loff_t pos_out, | ||
3068 | size_t *req_count, unsigned int flags) | ||
3069 | { | ||
3070 | struct inode *inode_in = file_inode(file_in); | ||
3071 | struct inode *inode_out = file_inode(file_out); | ||
3072 | uint64_t count = *req_count; | ||
3073 | loff_t size_in; | ||
3074 | int ret; | ||
3075 | |||
3076 | ret = generic_file_rw_checks(file_in, file_out); | ||
3077 | if (ret) | ||
3078 | return ret; | ||
3079 | |||
3080 | /* Don't touch certain kinds of inodes */ | ||
3081 | if (IS_IMMUTABLE(inode_out)) | ||
3082 | return -EPERM; | ||
3083 | |||
3084 | if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out)) | ||
3085 | return -ETXTBSY; | ||
3086 | |||
3087 | /* Ensure offsets don't wrap. */ | ||
3088 | if (pos_in + count < pos_in || pos_out + count < pos_out) | ||
3089 | return -EOVERFLOW; | ||
3090 | |||
3091 | /* Shorten the copy to EOF */ | ||
3092 | size_in = i_size_read(inode_in); | ||
3093 | if (pos_in >= size_in) | ||
3094 | count = 0; | ||
3095 | else | ||
3096 | count = min(count, size_in - (uint64_t)pos_in); | ||
3097 | |||
3098 | ret = generic_write_check_limits(file_out, pos_out, &count); | ||
3099 | if (ret) | ||
3100 | return ret; | ||
3101 | |||
3102 | /* Don't allow overlapped copying within the same file. */ | ||
3103 | if (inode_in == inode_out && | ||
3104 | pos_out + count > pos_in && | ||
3105 | pos_out < pos_in + count) | ||
3106 | return -EINVAL; | ||
3107 | |||
3108 | *req_count = count; | ||
3109 | return 0; | ||
3110 | } | ||
3111 | |||
3059 | int pagecache_write_begin(struct file *file, struct address_space *mapping, | 3112 | int pagecache_write_begin(struct file *file, struct address_space *mapping, |
3060 | loff_t pos, unsigned len, unsigned flags, | 3113 | loff_t pos, unsigned len, unsigned flags, |
3061 | struct page **pagep, void **fsdata) | 3114 | struct page **pagep, void **fsdata) |