Skip to content

Commit 5ae92b3

Browse files
jiangliueryugey
authored andcommitted
ptfs: refine implementation of seal_size_check()
Refine implementation of seal_size_check() with better handling of fallocate flags. Signed-off-by: Jiang Liu <[email protected]>
1 parent 8b03121 commit 5ae92b3

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/passthrough/mod.rs

+26-21
Original file line numberDiff line numberDiff line change
@@ -813,30 +813,35 @@ impl<S: BitmapSlice + Send + Sync> PassthroughFs<S> {
813813

814814
match opcode {
815815
// write should not exceed the file size.
816-
Opcode::Write if size + offset > file_size => Err(eperm()),
817-
818-
// fallocate operation should not allocate blocks exceed the file size.
819-
//
820-
// FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE mode will change file size which
821-
// is not allowed.
822-
//
823-
// FALLOC_FL_PUNCH_HOLE mode won't change file size, as it must be ORed with
824-
// FALLOC_FL_KEEP_SIZE.
825-
Opcode::Fallocate
826-
if ((mode == 0
827-
|| mode == libc::FALLOC_FL_KEEP_SIZE
828-
|| mode & libc::FALLOC_FL_ZERO_RANGE != 0)
829-
&& size + offset > file_size)
830-
|| (mode & libc::FALLOC_FL_COLLAPSE_RANGE != 0
831-
|| mode & libc::FALLOC_FL_INSERT_RANGE != 0) =>
832-
{
833-
Err(eperm())
816+
Opcode::Write => {
817+
if size + offset > file_size {
818+
return Err(eperm());
819+
}
834820
}
835821

836-
// setattr operation should be handled in setattr handler, other operations won't
837-
// change file size.
838-
_ => Ok(()),
822+
Opcode::Fallocate => {
823+
let op = mode & !(libc::FALLOC_FL_KEEP_SIZE | libc::FALLOC_FL_UNSHARE_RANGE);
824+
match op {
825+
// Allocate, punch and zero, must not change file size.
826+
0 | libc::FALLOC_FL_PUNCH_HOLE | libc::FALLOC_FL_ZERO_RANGE => {
827+
if size + offset > file_size {
828+
return Err(eperm());
829+
}
830+
}
831+
// collapse and insert will change file size, forbid.
832+
libc::FALLOC_FL_COLLAPSE_RANGE | libc::FALLOC_FL_INSERT_RANGE => {
833+
return Err(eperm());
834+
}
835+
// Invalid operation
836+
_ => return Err(einval()),
837+
}
838+
}
839+
840+
// setattr operation should be handled in setattr handler.
841+
_ => return Err(enosys()),
839842
}
843+
844+
Ok(())
840845
}
841846

842847
fn get_writeback_open_flags(&self, flags: i32) -> i32 {

0 commit comments

Comments
 (0)