cifs: fix fallocate when trying to allocate a hole.

Remove the conditional checking for out_data_len and skipping the fallocate
if it is 0. This is wrong will actually change any legitimate the fallocate
where the entire region is unallocated into a no-op.

Additionally, before allocating the range, if FALLOC_FL_KEEP_SIZE is set then
we need to clamp the length of the fallocate region as to not extend the size of the file.

Fixes: 966a3cb7c7db ("cifs: improve fallocate emulation")
Signed-off-by: Ronnie Sahlberg <lsahlber@redhat.com>
Signed-off-by: Steve French <stfrench@microsoft.com>

authored by Ronnie Sahlberg and committed by Steve French 488968a8 7b09d4e0

+18 -5
+18 -5
fs/cifs/smb2ops.c
··· 3667 (char **)&out_data, &out_data_len); 3668 if (rc) 3669 goto out; 3670 - /* 3671 - * It is already all allocated 3672 - */ 3673 - if (out_data_len == 0) 3674 - goto out; 3675 3676 buf = kzalloc(1024 * 1024, GFP_KERNEL); 3677 if (buf == NULL) { ··· 3787 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) { 3788 rc = 0; 3789 goto out; 3790 } 3791 3792 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
··· 3667 (char **)&out_data, &out_data_len); 3668 if (rc) 3669 goto out; 3670 3671 buf = kzalloc(1024 * 1024, GFP_KERNEL); 3672 if (buf == NULL) { ··· 3792 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) { 3793 rc = 0; 3794 goto out; 3795 + } 3796 + 3797 + if (keep_size == true) { 3798 + /* 3799 + * We can not preallocate pages beyond the end of the file 3800 + * in SMB2 3801 + */ 3802 + if (off >= i_size_read(inode)) { 3803 + rc = 0; 3804 + goto out; 3805 + } 3806 + /* 3807 + * For fallocates that are partially beyond the end of file, 3808 + * clamp len so we only fallocate up to the end of file. 3809 + */ 3810 + if (off + len > i_size_read(inode)) { 3811 + len = i_size_read(inode) - off; 3812 + } 3813 } 3814 3815 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {