Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * linux/fs/ext2/ioctl.c
0004  *
0005  * Copyright (C) 1993, 1994, 1995
0006  * Remy Card (card@masi.ibp.fr)
0007  * Laboratoire MASI - Institut Blaise Pascal
0008  * Universite Pierre et Marie Curie (Paris VI)
0009  */
0010 
0011 #include "ext2.h"
0012 #include <linux/capability.h>
0013 #include <linux/time.h>
0014 #include <linux/sched.h>
0015 #include <linux/compat.h>
0016 #include <linux/mount.h>
0017 #include <asm/current.h>
0018 #include <linux/uaccess.h>
0019 #include <linux/fileattr.h>
0020 
0021 int ext2_fileattr_get(struct dentry *dentry, struct fileattr *fa)
0022 {
0023     struct ext2_inode_info *ei = EXT2_I(d_inode(dentry));
0024 
0025     fileattr_fill_flags(fa, ei->i_flags & EXT2_FL_USER_VISIBLE);
0026 
0027     return 0;
0028 }
0029 
0030 int ext2_fileattr_set(struct user_namespace *mnt_userns,
0031               struct dentry *dentry, struct fileattr *fa)
0032 {
0033     struct inode *inode = d_inode(dentry);
0034     struct ext2_inode_info *ei = EXT2_I(inode);
0035 
0036     if (fileattr_has_fsx(fa))
0037         return -EOPNOTSUPP;
0038 
0039     /* Is it quota file? Do not allow user to mess with it */
0040     if (IS_NOQUOTA(inode))
0041         return -EPERM;
0042 
0043     ei->i_flags = (ei->i_flags & ~EXT2_FL_USER_MODIFIABLE) |
0044         (fa->flags & EXT2_FL_USER_MODIFIABLE);
0045 
0046     ext2_set_inode_flags(inode);
0047     inode->i_ctime = current_time(inode);
0048     mark_inode_dirty(inode);
0049 
0050     return 0;
0051 }
0052 
0053 
0054 long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
0055 {
0056     struct inode *inode = file_inode(filp);
0057     struct ext2_inode_info *ei = EXT2_I(inode);
0058     unsigned short rsv_window_size;
0059     int ret;
0060 
0061     ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
0062 
0063     switch (cmd) {
0064     case EXT2_IOC_GETVERSION:
0065         return put_user(inode->i_generation, (int __user *) arg);
0066     case EXT2_IOC_SETVERSION: {
0067         __u32 generation;
0068 
0069         if (!inode_owner_or_capable(&init_user_ns, inode))
0070             return -EPERM;
0071         ret = mnt_want_write_file(filp);
0072         if (ret)
0073             return ret;
0074         if (get_user(generation, (int __user *) arg)) {
0075             ret = -EFAULT;
0076             goto setversion_out;
0077         }
0078 
0079         inode_lock(inode);
0080         inode->i_ctime = current_time(inode);
0081         inode->i_generation = generation;
0082         inode_unlock(inode);
0083 
0084         mark_inode_dirty(inode);
0085 setversion_out:
0086         mnt_drop_write_file(filp);
0087         return ret;
0088     }
0089     case EXT2_IOC_GETRSVSZ:
0090         if (test_opt(inode->i_sb, RESERVATION)
0091             && S_ISREG(inode->i_mode)
0092             && ei->i_block_alloc_info) {
0093             rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
0094             return put_user(rsv_window_size, (int __user *)arg);
0095         }
0096         return -ENOTTY;
0097     case EXT2_IOC_SETRSVSZ: {
0098 
0099         if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
0100             return -ENOTTY;
0101 
0102         if (!inode_owner_or_capable(&init_user_ns, inode))
0103             return -EACCES;
0104 
0105         if (get_user(rsv_window_size, (int __user *)arg))
0106             return -EFAULT;
0107 
0108         ret = mnt_want_write_file(filp);
0109         if (ret)
0110             return ret;
0111 
0112         if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
0113             rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
0114 
0115         /*
0116          * need to allocate reservation structure for this inode
0117          * before set the window size
0118          */
0119         /*
0120          * XXX What lock should protect the rsv_goal_size?
0121          * Accessed in ext2_get_block only.  ext3 uses i_truncate.
0122          */
0123         mutex_lock(&ei->truncate_mutex);
0124         if (!ei->i_block_alloc_info)
0125             ext2_init_block_alloc_info(inode);
0126 
0127         if (ei->i_block_alloc_info){
0128             struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
0129             rsv->rsv_goal_size = rsv_window_size;
0130         } else {
0131             ret = -ENOMEM;
0132         }
0133 
0134         mutex_unlock(&ei->truncate_mutex);
0135         mnt_drop_write_file(filp);
0136         return ret;
0137     }
0138     default:
0139         return -ENOTTY;
0140     }
0141 }
0142 
0143 #ifdef CONFIG_COMPAT
0144 long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0145 {
0146     /* These are just misnamed, they actually get/put from/to user an int */
0147     switch (cmd) {
0148     case EXT2_IOC32_GETVERSION:
0149         cmd = EXT2_IOC_GETVERSION;
0150         break;
0151     case EXT2_IOC32_SETVERSION:
0152         cmd = EXT2_IOC_SETVERSION;
0153         break;
0154     default:
0155         return -ENOIOCTLCMD;
0156     }
0157     return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
0158 }
0159 #endif