forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kernel+LibC: Add posix_fallocate syscall
- Loading branch information
1 parent
ad904cd
commit d783389
Showing
6 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2022, Leon Albrecht <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <AK/Checked.h> | ||
#include <Kernel/FileSystem/Inode.h> | ||
#include <Kernel/FileSystem/InodeFile.h> | ||
#include <Kernel/FileSystem/OpenFileDescription.h> | ||
#include <Kernel/Process.h> | ||
|
||
namespace Kernel { | ||
|
||
ErrorOr<FlatPtr> Process::sys$posix_fallocate(int fd, Userspace<off_t const*> userspace_offset, Userspace<off_t const*> userspace_length) | ||
{ | ||
VERIFY_NO_PROCESS_BIG_LOCK(this); | ||
TRY(require_promise(Pledge::stdio)); | ||
|
||
auto offset = TRY(copy_typed_from_user(userspace_offset)); | ||
if (offset < 0) | ||
return EINVAL; | ||
auto length = TRY(copy_typed_from_user(userspace_length)); | ||
if (length <= 0) | ||
return EINVAL; | ||
|
||
Checked<size_t> checked_size { length }; | ||
checked_size += offset; | ||
// FIXME: Return EFBIG if offset+length > FileSizeMax | ||
if (checked_size.has_overflow()) | ||
return EFBIG; | ||
|
||
auto description = TRY(open_file_description(fd)); | ||
if (!description->is_writable()) | ||
return EBADF; | ||
|
||
if (description->is_fifo()) | ||
return ESPIPE; | ||
|
||
if (!S_ISREG(TRY(description->file().stat()).st_mode)) | ||
return ENODEV; | ||
|
||
VERIFY(description->file().is_inode()); | ||
|
||
auto& file = static_cast<InodeFile&>(description->file()); | ||
if (file.inode().size() >= checked_size.value()) | ||
return 0; | ||
|
||
// Note: truncate essentially calls resize in the inodes implementation | ||
// while resize is not a standard member of an inode, so we just call | ||
// truncate instead | ||
TRY(file.inode().truncate(checked_size.value())); | ||
|
||
// FIXME: ENOSPC: There is not enough space left on the device containing the file referred to by fd. | ||
// FIXME: EINTR: A signal was caught during execution. | ||
return 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters