Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * linux/fs/jfs/ioctl.c
0004  *
0005  * Copyright (C) 2006 Herbert Poetzl
0006  * adapted from Remy Card's ext2/ioctl.c
0007  */
0008 
0009 #include <linux/fs.h>
0010 #include <linux/ctype.h>
0011 #include <linux/capability.h>
0012 #include <linux/mount.h>
0013 #include <linux/time.h>
0014 #include <linux/sched.h>
0015 #include <linux/blkdev.h>
0016 #include <asm/current.h>
0017 #include <linux/uaccess.h>
0018 #include <linux/fileattr.h>
0019 
0020 #include "jfs_filsys.h"
0021 #include "jfs_debug.h"
0022 #include "jfs_incore.h"
0023 #include "jfs_dinode.h"
0024 #include "jfs_inode.h"
0025 #include "jfs_dmap.h"
0026 #include "jfs_discard.h"
0027 
0028 static struct {
0029     long jfs_flag;
0030     long ext2_flag;
0031 } jfs_map[] = {
0032     {JFS_NOATIME_FL,    FS_NOATIME_FL},
0033     {JFS_DIRSYNC_FL,    FS_DIRSYNC_FL},
0034     {JFS_SYNC_FL,       FS_SYNC_FL},
0035     {JFS_SECRM_FL,      FS_SECRM_FL},
0036     {JFS_UNRM_FL,       FS_UNRM_FL},
0037     {JFS_APPEND_FL,     FS_APPEND_FL},
0038     {JFS_IMMUTABLE_FL,  FS_IMMUTABLE_FL},
0039     {0, 0},
0040 };
0041 
0042 static long jfs_map_ext2(unsigned long flags, int from)
0043 {
0044     int index=0;
0045     long mapped=0;
0046 
0047     while (jfs_map[index].jfs_flag) {
0048         if (from) {
0049             if (jfs_map[index].ext2_flag & flags)
0050                 mapped |= jfs_map[index].jfs_flag;
0051         } else {
0052             if (jfs_map[index].jfs_flag & flags)
0053                 mapped |= jfs_map[index].ext2_flag;
0054         }
0055         index++;
0056     }
0057     return mapped;
0058 }
0059 
0060 int jfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
0061 {
0062     struct jfs_inode_info *jfs_inode = JFS_IP(d_inode(dentry));
0063     unsigned int flags = jfs_inode->mode2 & JFS_FL_USER_VISIBLE;
0064 
0065     if (d_is_special(dentry))
0066         return -ENOTTY;
0067 
0068     fileattr_fill_flags(fa, jfs_map_ext2(flags, 0));
0069 
0070     return 0;
0071 }
0072 
0073 int jfs_fileattr_set(struct user_namespace *mnt_userns,
0074              struct dentry *dentry, struct fileattr *fa)
0075 {
0076     struct inode *inode = d_inode(dentry);
0077     struct jfs_inode_info *jfs_inode = JFS_IP(inode);
0078     unsigned int flags;
0079 
0080     if (d_is_special(dentry))
0081         return -ENOTTY;
0082 
0083     if (fileattr_has_fsx(fa))
0084         return -EOPNOTSUPP;
0085 
0086     flags = jfs_map_ext2(fa->flags, 1);
0087     if (!S_ISDIR(inode->i_mode))
0088         flags &= ~JFS_DIRSYNC_FL;
0089 
0090     /* Is it quota file? Do not allow user to mess with it */
0091     if (IS_NOQUOTA(inode))
0092         return -EPERM;
0093 
0094     flags = flags & JFS_FL_USER_MODIFIABLE;
0095     flags |= jfs_inode->mode2 & ~JFS_FL_USER_MODIFIABLE;
0096     jfs_inode->mode2 = flags;
0097 
0098     jfs_set_inode_flags(inode);
0099     inode->i_ctime = current_time(inode);
0100     mark_inode_dirty(inode);
0101 
0102     return 0;
0103 }
0104 
0105 long jfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
0106 {
0107     struct inode *inode = file_inode(filp);
0108 
0109     switch (cmd) {
0110     case FITRIM:
0111     {
0112         struct super_block *sb = inode->i_sb;
0113         struct fstrim_range range;
0114         s64 ret = 0;
0115 
0116         if (!capable(CAP_SYS_ADMIN))
0117             return -EPERM;
0118 
0119         if (!bdev_max_discard_sectors(sb->s_bdev)) {
0120             jfs_warn("FITRIM not supported on device");
0121             return -EOPNOTSUPP;
0122         }
0123 
0124         if (copy_from_user(&range, (struct fstrim_range __user *)arg,
0125             sizeof(range)))
0126             return -EFAULT;
0127 
0128         range.minlen = max_t(unsigned int, range.minlen,
0129                      bdev_discard_granularity(sb->s_bdev));
0130 
0131         ret = jfs_ioc_trim(inode, &range);
0132         if (ret < 0)
0133             return ret;
0134 
0135         if (copy_to_user((struct fstrim_range __user *)arg, &range,
0136             sizeof(range)))
0137             return -EFAULT;
0138 
0139         return 0;
0140     }
0141 
0142     default:
0143         return -ENOTTY;
0144     }
0145 }