Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add do_ftruncate that truncates a struct file

do_sys_ftruncate receives a file descriptor, fgets the struct file, and
finally actually truncates the file.

do_ftruncate allows for passing in a file directly, with the caller
already holding a reference to it.

Signed-off-by: Tony Solomonik <tony.solomonik@gmail.com>
Reviewed-by: Christian Brauner <brauner@kernel.org>
Link: https://lore.kernel.org/r/20240202121724.17461-2-tony.solomonik@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Tony Solomonik and committed by
Jens Axboe
5f0d594c a6e959bd

+35 -31
+1
fs/internal.h
··· 183 183 extern int build_open_flags(const struct open_how *how, struct open_flags *op); 184 184 struct file *file_close_fd_locked(struct files_struct *files, unsigned fd); 185 185 186 + long do_ftruncate(struct file *file, loff_t length, int small); 186 187 long do_sys_ftruncate(unsigned int fd, loff_t length, int small); 187 188 int chmod_common(const struct path *path, umode_t mode); 188 189 int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
+34 -31
fs/open.c
··· 154 154 } 155 155 #endif 156 156 157 - long do_sys_ftruncate(unsigned int fd, loff_t length, int small) 157 + long do_ftruncate(struct file *file, loff_t length, int small) 158 158 { 159 159 struct inode *inode; 160 160 struct dentry *dentry; 161 + int error; 162 + 163 + /* explicitly opened as large or we are on 64-bit box */ 164 + if (file->f_flags & O_LARGEFILE) 165 + small = 0; 166 + 167 + dentry = file->f_path.dentry; 168 + inode = dentry->d_inode; 169 + if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE)) 170 + return -EINVAL; 171 + 172 + /* Cannot ftruncate over 2^31 bytes without large file support */ 173 + if (small && length > MAX_NON_LFS) 174 + return -EINVAL; 175 + 176 + /* Check IS_APPEND on real upper inode */ 177 + if (IS_APPEND(file_inode(file))) 178 + return -EPERM; 179 + sb_start_write(inode->i_sb); 180 + error = security_file_truncate(file); 181 + if (!error) 182 + error = do_truncate(file_mnt_idmap(file), dentry, length, 183 + ATTR_MTIME | ATTR_CTIME, file); 184 + sb_end_write(inode->i_sb); 185 + 186 + return error; 187 + } 188 + 189 + long do_sys_ftruncate(unsigned int fd, loff_t length, int small) 190 + { 161 191 struct fd f; 162 192 int error; 163 193 164 - error = -EINVAL; 165 194 if (length < 0) 166 - goto out; 167 - error = -EBADF; 195 + return -EINVAL; 168 196 f = fdget(fd); 169 197 if (!f.file) 170 - goto out; 198 + return -EBADF; 171 199 172 - /* explicitly opened as large or we are on 64-bit box */ 173 - if (f.file->f_flags & O_LARGEFILE) 174 - small = 0; 200 + error = do_ftruncate(f.file, length, small); 175 201 176 - dentry = f.file->f_path.dentry; 177 - inode = dentry->d_inode; 178 - error = -EINVAL; 179 - if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE)) 180 - goto out_putf; 181 - 182 - error = -EINVAL; 183 - /* Cannot ftruncate over 2^31 bytes without large file support */ 184 - if (small && length > MAX_NON_LFS) 185 - goto out_putf; 186 - 187 - error = -EPERM; 188 - /* Check IS_APPEND on real upper inode */ 189 - if (IS_APPEND(file_inode(f.file))) 190 - goto out_putf; 191 - sb_start_write(inode->i_sb); 192 - error = security_file_truncate(f.file); 193 - if (!error) 194 - error = do_truncate(file_mnt_idmap(f.file), dentry, length, 195 - ATTR_MTIME | ATTR_CTIME, f.file); 196 - sb_end_write(inode->i_sb); 197 - out_putf: 198 202 fdput(f); 199 - out: 200 203 return error; 201 204 } 202 205