exfat: fix appending discontinuous clusters to empty file
authorYuezhang Mo <Yuezhang.Mo@sony.com>
Fri, 16 Feb 2024 12:19:55 +0000 (20:19 +0800)
committerNamjae Jeon <linkinjeon@kernel.org>
Sun, 18 Feb 2024 05:41:18 +0000 (14:41 +0900)
commit3a7845041eb7235f2fb00ef0960995da5be63b11
tree7bf4953fad0f8f1ac6362ab222592a123d3a5033
parentc02197fc9076e7d991c8f6adc11759c5ba52ddc6
exfat: fix appending discontinuous clusters to empty file

Eric Hong found that when using ftruncate to expand an empty file,
exfat_ent_set() will fail if discontinuous clusters are allocated.
The reason is that the empty file does not have a cluster chain,
but exfat_ent_set() attempts to append the newly allocated cluster
to the cluster chain. In addition, exfat_find_last_cluster() only
supports finding the last cluster in a non-empty file.

So this commit adds a check whether the file is empty. If the file
is empty, exfat_find_last_cluster() and exfat_ent_set() are no longer
called as they do not need to be called.

Fixes: f55c096f62f1 ("exfat: do not zero the extended part")
Reported-by: Eric Hong <erichong@qnap.com>
Signed-off-by: Yuezhang Mo <Yuezhang.Mo@sony.com>
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
fs/exfat/file.c