diff --git a/file.c b/file.c index 557abc9..7e51646 100644 --- a/file.c +++ b/file.c @@ -122,26 +122,6 @@ static int simplefs_write_begin(const struct kiocb *iocb, void **fsdata) { struct file *file = iocb->ki_filp; - struct simplefs_sb_info *sbi = SIMPLEFS_SB(file->f_inode->i_sb); - int err; - uint32_t nr_allocs = 0; - - if (pos + len > SIMPLEFS_MAX_FILESIZE) - return -ENOSPC; - - nr_allocs = max(pos + len, file->f_inode->i_size) / SIMPLEFS_BLOCK_SIZE; - if (nr_allocs > file->f_inode->i_blocks - 1) - nr_allocs -= file->f_inode->i_blocks - 1; - else - nr_allocs = 0; - if (nr_allocs > sbi->nr_free_blocks) - return -ENOSPC; - - err = block_write_begin(mapping, pos, len, foliop, simplefs_file_get_block); - if (err < 0) - pr_err("newly allocated blocks reclaim not implemented yet\n"); - return err; -} #elif SIMPLEFS_AT_LEAST(6, 12, 0) static int simplefs_write_begin(struct file *file, struct address_space *mapping, @@ -150,26 +130,6 @@ static int simplefs_write_begin(struct file *file, struct folio **foliop, void **fsdata) { - struct simplefs_sb_info *sbi = SIMPLEFS_SB(file->f_inode->i_sb); - int err; - uint32_t nr_allocs = 0; - - if (pos + len > SIMPLEFS_MAX_FILESIZE) - return -ENOSPC; - - nr_allocs = max(pos + len, file->f_inode->i_size) / SIMPLEFS_BLOCK_SIZE; - if (nr_allocs > file->f_inode->i_blocks - 1) - nr_allocs -= file->f_inode->i_blocks - 1; - else - nr_allocs = 0; - if (nr_allocs > sbi->nr_free_blocks) - return -ENOSPC; - - err = block_write_begin(mapping, pos, len, foliop, simplefs_file_get_block); - if (err < 0) - pr_err("newly allocated blocks reclaim not implemented yet\n"); - return err; -} #elif SIMPLEFS_AT_LEAST(5, 19, 0) static int simplefs_write_begin(struct file *file, struct address_space *mapping, @@ -178,26 +138,6 @@ static int simplefs_write_begin(struct file *file, struct page **pagep, void **fsdata) { - struct simplefs_sb_info *sbi = SIMPLEFS_SB(file->f_inode->i_sb); - int err; - uint32_t nr_allocs = 0; - - if (pos + len > SIMPLEFS_MAX_FILESIZE) - return -ENOSPC; - - nr_allocs = max(pos + len, file->f_inode->i_size) / SIMPLEFS_BLOCK_SIZE; - if (nr_allocs > file->f_inode->i_blocks - 1) - nr_allocs -= file->f_inode->i_blocks - 1; - else - nr_allocs = 0; - if (nr_allocs > sbi->nr_free_blocks) - return -ENOSPC; - - err = block_write_begin(mapping, pos, len, pagep, simplefs_file_get_block); - if (err < 0) - pr_err("newly allocated blocks reclaim not implemented yet\n"); - return err; -} #else static int simplefs_write_begin(struct file *file, struct address_space *mapping, @@ -207,6 +147,7 @@ static int simplefs_write_begin(struct file *file, struct page **pagep, void **fsdata) { +#endif struct simplefs_sb_info *sbi = SIMPLEFS_SB(file->f_inode->i_sb); int err; uint32_t nr_allocs = 0; @@ -221,14 +162,18 @@ static int simplefs_write_begin(struct file *file, nr_allocs = 0; if (nr_allocs > sbi->nr_free_blocks) return -ENOSPC; - +#if SIMPLEFS_AT_LEAST(6, 12, 0) + err = block_write_begin(mapping, pos, len, foliop, simplefs_file_get_block); +#elif SIMPLEFS_AT_LEAST(5, 19, 0) + err = block_write_begin(mapping, pos, len, pagep, simplefs_file_get_block); +#else err = block_write_begin(mapping, pos, len, flags, pagep, simplefs_file_get_block); +#endif if (err < 0) pr_err("newly allocated blocks reclaim not implemented yet\n"); return err; } -#endif /* Called by the VFS after writing data from a write() syscall to the page * cache. This function updates inode metadata and truncates the file if diff --git a/simplefs.h b/simplefs.h index 1ee01c7..c7aa3c5 100644 --- a/simplefs.h +++ b/simplefs.h @@ -64,9 +64,9 @@ struct simplefs_inode { #include /* compatibility macros */ #define SIMPLEFS_AT_LEAST(major, minor, rev) \ - LINUX_VERSION_CODE >= KERNEL_VERSION(major, minor, rev) + (LINUX_VERSION_CODE >= KERNEL_VERSION(major, minor, rev)) #define SIMPLEFS_LESS_EQUAL(major, minor, rev) \ - LINUX_VERSION_CODE <= KERNEL_VERSION(major, minor, rev) + (LINUX_VERSION_CODE <= KERNEL_VERSION(major, minor, rev)) /* A 'container' structure that keeps the VFS inode and additional on-disk * data.