blob: 32a8d10b579df71a75ee13b645ece202e167d908 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/ext2/ioctl.c
4 *
5 * Copyright (C) 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
9 */
10
11#include "ext2.h"
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080012#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/time.h>
14#include <linux/sched.h>
David Howellse322ff02006-08-29 19:06:20 +010015#include <linux/compat.h>
Dave Hansen42a74f22008-02-15 14:37:46 -080016#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/current.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080018#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20
Andi Kleen14f9f7b2008-02-06 01:40:10 -080021long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
Al Viro496ad9a2013-01-23 17:07:38 -050023 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 struct ext2_inode_info *ei = EXT2_I(inode);
25 unsigned int flags;
Martin J. Bligha686cd82007-10-16 23:30:46 -070026 unsigned short rsv_window_size;
Dave Hansen42a74f22008-02-15 14:37:46 -080027 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29 ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
30
31 switch (cmd) {
32 case EXT2_IOC_GETFLAGS:
33 flags = ei->i_flags & EXT2_FL_USER_VISIBLE;
34 return put_user(flags, (int __user *) arg);
35 case EXT2_IOC_SETFLAGS: {
36 unsigned int oldflags;
37
Al Viroa561be72011-11-23 11:57:51 -050038 ret = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080039 if (ret)
40 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Serge E. Hallyn2e149672011-03-23 16:43:26 -070042 if (!inode_owner_or_capable(inode)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080043 ret = -EACCES;
44 goto setflags_out;
45 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Dave Hansen42a74f22008-02-15 14:37:46 -080047 if (get_user(flags, (int __user *) arg)) {
48 ret = -EFAULT;
49 goto setflags_out;
50 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Duane Griffinef8b6462009-01-07 18:07:21 -080052 flags = ext2_mask_flags(inode->i_mode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Al Viro59551022016-01-22 15:40:57 -050054 inode_lock(inode);
Jan Karae47776a2007-11-14 16:58:56 -080055 /* Is it quota file? Do not allow user to mess with it */
56 if (IS_NOQUOTA(inode)) {
Al Viro59551022016-01-22 15:40:57 -050057 inode_unlock(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -080058 ret = -EPERM;
59 goto setflags_out;
Jan Karae47776a2007-11-14 16:58:56 -080060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 oldflags = ei->i_flags;
62
Darrick J. Wong5aca2842019-07-01 08:25:34 -070063 ret = vfs_ioc_setflags_prepare(inode, oldflags, flags);
64 if (ret) {
65 inode_unlock(inode);
66 goto setflags_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
68
69 flags = flags & EXT2_FL_USER_MODIFIABLE;
70 flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
71 ei->i_flags = flags;
72
73 ext2_set_inode_flags(inode);
Deepa Dinamani02027d42016-09-14 07:48:05 -070074 inode->i_ctime = current_time(inode);
Al Viro59551022016-01-22 15:40:57 -050075 inode_unlock(inode);
Djalal Harouni34b07842012-01-09 15:58:37 +010076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -080078setflags_out:
Al Viro2a79f172011-12-09 08:06:57 -050079 mnt_drop_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080080 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82 case EXT2_IOC_GETVERSION:
83 return put_user(inode->i_generation, (int __user *) arg);
Djalal Harouni34b07842012-01-09 15:58:37 +010084 case EXT2_IOC_SETVERSION: {
85 __u32 generation;
86
Serge E. Hallyn2e149672011-03-23 16:43:26 -070087 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return -EPERM;
Al Viroa561be72011-11-23 11:57:51 -050089 ret = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080090 if (ret)
91 return ret;
Djalal Harouni34b07842012-01-09 15:58:37 +010092 if (get_user(generation, (int __user *) arg)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080093 ret = -EFAULT;
Djalal Harouni34b07842012-01-09 15:58:37 +010094 goto setversion_out;
Dave Hansen42a74f22008-02-15 14:37:46 -080095 }
Djalal Harouni34b07842012-01-09 15:58:37 +010096
Al Viro59551022016-01-22 15:40:57 -050097 inode_lock(inode);
Deepa Dinamani02027d42016-09-14 07:48:05 -070098 inode->i_ctime = current_time(inode);
Djalal Harouni34b07842012-01-09 15:58:37 +010099 inode->i_generation = generation;
Al Viro59551022016-01-22 15:40:57 -0500100 inode_unlock(inode);
Djalal Harouni34b07842012-01-09 15:58:37 +0100101
102 mark_inode_dirty(inode);
103setversion_out:
Al Viro2a79f172011-12-09 08:06:57 -0500104 mnt_drop_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800105 return ret;
Djalal Harouni34b07842012-01-09 15:58:37 +0100106 }
Martin J. Bligha686cd82007-10-16 23:30:46 -0700107 case EXT2_IOC_GETRSVSZ:
108 if (test_opt(inode->i_sb, RESERVATION)
109 && S_ISREG(inode->i_mode)
110 && ei->i_block_alloc_info) {
111 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
112 return put_user(rsv_window_size, (int __user *)arg);
113 }
114 return -ENOTTY;
115 case EXT2_IOC_SETRSVSZ: {
116
117 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
118 return -ENOTTY;
119
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700120 if (!inode_owner_or_capable(inode))
Martin J. Bligha686cd82007-10-16 23:30:46 -0700121 return -EACCES;
122
123 if (get_user(rsv_window_size, (int __user *)arg))
124 return -EFAULT;
125
Al Viroa561be72011-11-23 11:57:51 -0500126 ret = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800127 if (ret)
128 return ret;
129
Martin J. Bligha686cd82007-10-16 23:30:46 -0700130 if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
131 rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
132
133 /*
134 * need to allocate reservation structure for this inode
135 * before set the window size
136 */
137 /*
138 * XXX What lock should protect the rsv_goal_size?
139 * Accessed in ext2_get_block only. ext3 uses i_truncate.
140 */
141 mutex_lock(&ei->truncate_mutex);
142 if (!ei->i_block_alloc_info)
143 ext2_init_block_alloc_info(inode);
144
145 if (ei->i_block_alloc_info){
146 struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
147 rsv->rsv_goal_size = rsv_window_size;
Chengguang Xuacd1f042019-10-23 21:56:43 +0800148 } else {
149 ret = -ENOMEM;
Martin J. Bligha686cd82007-10-16 23:30:46 -0700150 }
Chengguang Xuacd1f042019-10-23 21:56:43 +0800151
Martin J. Bligha686cd82007-10-16 23:30:46 -0700152 mutex_unlock(&ei->truncate_mutex);
Al Viro2a79f172011-12-09 08:06:57 -0500153 mnt_drop_write_file(filp);
Chengguang Xuacd1f042019-10-23 21:56:43 +0800154 return ret;
Martin J. Bligha686cd82007-10-16 23:30:46 -0700155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 default:
157 return -ENOTTY;
158 }
159}
David Howellse322ff02006-08-29 19:06:20 +0100160
161#ifdef CONFIG_COMPAT
162long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
163{
David Howellse322ff02006-08-29 19:06:20 +0100164 /* These are just misnamed, they actually get/put from/to user an int */
165 switch (cmd) {
166 case EXT2_IOC32_GETFLAGS:
167 cmd = EXT2_IOC_GETFLAGS;
168 break;
169 case EXT2_IOC32_SETFLAGS:
170 cmd = EXT2_IOC_SETFLAGS;
171 break;
172 case EXT2_IOC32_GETVERSION:
173 cmd = EXT2_IOC_GETVERSION;
174 break;
175 case EXT2_IOC32_SETVERSION:
176 cmd = EXT2_IOC_SETVERSION;
177 break;
178 default:
179 return -ENOIOCTLCMD;
180 }
Andi Kleen14f9f7b2008-02-06 01:40:10 -0800181 return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
David Howellse322ff02006-08-29 19:06:20 +0100182}
183#endif