blob: b386af2e45f41fda4f965eafa5751ab10dc48bc3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ext2/ioctl.c
3 *
4 * Copyright (C) 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 */
9
10#include "ext2.h"
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080011#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/time.h>
13#include <linux/sched.h>
David Howellse322ff02006-08-29 19:06:20 +010014#include <linux/compat.h>
Dave Hansen42a74f22008-02-15 14:37:46 -080015#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/current.h>
17#include <asm/uaccess.h>
18
19
Andi Kleen14f9f7b2008-02-06 01:40:10 -080020long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Al Viro496ad9a2013-01-23 17:07:38 -050022 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 struct ext2_inode_info *ei = EXT2_I(inode);
24 unsigned int flags;
Martin J. Bligha686cd82007-10-16 23:30:46 -070025 unsigned short rsv_window_size;
Dave Hansen42a74f22008-02-15 14:37:46 -080026 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28 ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
29
30 switch (cmd) {
31 case EXT2_IOC_GETFLAGS:
Jan Kara4f99ed62007-05-08 00:31:04 -070032 ext2_get_inode_flags(ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 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
63 /*
64 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
65 * the relevant capability.
66 *
67 * This test looks nicer. Thanks to Pauline Middelink
68 */
69 if ((flags ^ oldflags) & (EXT2_APPEND_FL | EXT2_IMMUTABLE_FL)) {
Andrew Morton93f210d2006-12-06 20:37:33 -080070 if (!capable(CAP_LINUX_IMMUTABLE)) {
Al Viro59551022016-01-22 15:40:57 -050071 inode_unlock(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -080072 ret = -EPERM;
73 goto setflags_out;
Andrew Morton93f210d2006-12-06 20:37:33 -080074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76
77 flags = flags & EXT2_FL_USER_MODIFIABLE;
78 flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
79 ei->i_flags = flags;
80
81 ext2_set_inode_flags(inode);
82 inode->i_ctime = CURRENT_TIME_SEC;
Al Viro59551022016-01-22 15:40:57 -050083 inode_unlock(inode);
Djalal Harouni34b07842012-01-09 15:58:37 +010084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -080086setflags_out:
Al Viro2a79f172011-12-09 08:06:57 -050087 mnt_drop_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080088 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90 case EXT2_IOC_GETVERSION:
91 return put_user(inode->i_generation, (int __user *) arg);
Djalal Harouni34b07842012-01-09 15:58:37 +010092 case EXT2_IOC_SETVERSION: {
93 __u32 generation;
94
Serge E. Hallyn2e149672011-03-23 16:43:26 -070095 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return -EPERM;
Al Viroa561be72011-11-23 11:57:51 -050097 ret = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080098 if (ret)
99 return ret;
Djalal Harouni34b07842012-01-09 15:58:37 +0100100 if (get_user(generation, (int __user *) arg)) {
Dave Hansen42a74f22008-02-15 14:37:46 -0800101 ret = -EFAULT;
Djalal Harouni34b07842012-01-09 15:58:37 +0100102 goto setversion_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800103 }
Djalal Harouni34b07842012-01-09 15:58:37 +0100104
Al Viro59551022016-01-22 15:40:57 -0500105 inode_lock(inode);
Djalal Harouni34b07842012-01-09 15:58:37 +0100106 inode->i_ctime = CURRENT_TIME_SEC;
107 inode->i_generation = generation;
Al Viro59551022016-01-22 15:40:57 -0500108 inode_unlock(inode);
Djalal Harouni34b07842012-01-09 15:58:37 +0100109
110 mark_inode_dirty(inode);
111setversion_out:
Al Viro2a79f172011-12-09 08:06:57 -0500112 mnt_drop_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800113 return ret;
Djalal Harouni34b07842012-01-09 15:58:37 +0100114 }
Martin J. Bligha686cd82007-10-16 23:30:46 -0700115 case EXT2_IOC_GETRSVSZ:
116 if (test_opt(inode->i_sb, RESERVATION)
117 && S_ISREG(inode->i_mode)
118 && ei->i_block_alloc_info) {
119 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
120 return put_user(rsv_window_size, (int __user *)arg);
121 }
122 return -ENOTTY;
123 case EXT2_IOC_SETRSVSZ: {
124
125 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
126 return -ENOTTY;
127
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700128 if (!inode_owner_or_capable(inode))
Martin J. Bligha686cd82007-10-16 23:30:46 -0700129 return -EACCES;
130
131 if (get_user(rsv_window_size, (int __user *)arg))
132 return -EFAULT;
133
Al Viroa561be72011-11-23 11:57:51 -0500134 ret = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800135 if (ret)
136 return ret;
137
Martin J. Bligha686cd82007-10-16 23:30:46 -0700138 if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
139 rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
140
141 /*
142 * need to allocate reservation structure for this inode
143 * before set the window size
144 */
145 /*
146 * XXX What lock should protect the rsv_goal_size?
147 * Accessed in ext2_get_block only. ext3 uses i_truncate.
148 */
149 mutex_lock(&ei->truncate_mutex);
150 if (!ei->i_block_alloc_info)
151 ext2_init_block_alloc_info(inode);
152
153 if (ei->i_block_alloc_info){
154 struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
155 rsv->rsv_goal_size = rsv_window_size;
156 }
157 mutex_unlock(&ei->truncate_mutex);
Al Viro2a79f172011-12-09 08:06:57 -0500158 mnt_drop_write_file(filp);
Martin J. Bligha686cd82007-10-16 23:30:46 -0700159 return 0;
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 default:
162 return -ENOTTY;
163 }
164}
David Howellse322ff02006-08-29 19:06:20 +0100165
166#ifdef CONFIG_COMPAT
167long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
168{
David Howellse322ff02006-08-29 19:06:20 +0100169 /* These are just misnamed, they actually get/put from/to user an int */
170 switch (cmd) {
171 case EXT2_IOC32_GETFLAGS:
172 cmd = EXT2_IOC_GETFLAGS;
173 break;
174 case EXT2_IOC32_SETFLAGS:
175 cmd = EXT2_IOC_SETFLAGS;
176 break;
177 case EXT2_IOC32_GETVERSION:
178 cmd = EXT2_IOC_GETVERSION;
179 break;
180 case EXT2_IOC32_SETVERSION:
181 cmd = EXT2_IOC_SETVERSION;
182 break;
183 default:
184 return -ENOIOCTLCMD;
185 }
Andi Kleen14f9f7b2008-02-06 01:40:10 -0800186 return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
David Howellse322ff02006-08-29 19:06:20 +0100187}
188#endif