blob: e7431309bdca9b7c873a34718263f14097b81ff5 [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{
Andi Kleen14f9f7b2008-02-06 01:40:10 -080022 struct inode *inode = filp->f_dentry->d_inode;
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
Dave Hansen42a74f22008-02-15 14:37:46 -080038 ret = mnt_want_write(filp->f_path.mnt);
39 if (ret)
40 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Dave Hansen42a74f22008-02-15 14:37:46 -080042 if (!is_owner_or_cap(inode)) {
43 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
Andrew Morton93f210d2006-12-06 20:37:33 -080054 mutex_lock(&inode->i_mutex);
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)) {
57 mutex_unlock(&inode->i_mutex);
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)) {
71 mutex_unlock(&inode->i_mutex);
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;
Andrew Morton93f210d2006-12-06 20:37:33 -080080 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 ext2_set_inode_flags(inode);
83 inode->i_ctime = CURRENT_TIME_SEC;
84 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -080085setflags_out:
86 mnt_drop_write(filp->f_path.mnt);
87 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89 case EXT2_IOC_GETVERSION:
90 return put_user(inode->i_generation, (int __user *) arg);
91 case EXT2_IOC_SETVERSION:
Satyam Sharma3bd858a2007-07-17 15:00:08 +053092 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return -EPERM;
Dave Hansen42a74f22008-02-15 14:37:46 -080094 ret = mnt_want_write(filp->f_path.mnt);
95 if (ret)
96 return ret;
97 if (get_user(inode->i_generation, (int __user *) arg)) {
98 ret = -EFAULT;
99 } else {
100 inode->i_ctime = CURRENT_TIME_SEC;
101 mark_inode_dirty(inode);
102 }
103 mnt_drop_write(filp->f_path.mnt);
104 return ret;
Martin J. Bligha686cd82007-10-16 23:30:46 -0700105 case EXT2_IOC_GETRSVSZ:
106 if (test_opt(inode->i_sb, RESERVATION)
107 && S_ISREG(inode->i_mode)
108 && ei->i_block_alloc_info) {
109 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
110 return put_user(rsv_window_size, (int __user *)arg);
111 }
112 return -ENOTTY;
113 case EXT2_IOC_SETRSVSZ: {
114
115 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
116 return -ENOTTY;
117
Dave Hansen42a74f22008-02-15 14:37:46 -0800118 if (!is_owner_or_cap(inode))
Martin J. Bligha686cd82007-10-16 23:30:46 -0700119 return -EACCES;
120
121 if (get_user(rsv_window_size, (int __user *)arg))
122 return -EFAULT;
123
Dave Hansen42a74f22008-02-15 14:37:46 -0800124 ret = mnt_want_write(filp->f_path.mnt);
125 if (ret)
126 return ret;
127
Martin J. Bligha686cd82007-10-16 23:30:46 -0700128 if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
129 rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
130
131 /*
132 * need to allocate reservation structure for this inode
133 * before set the window size
134 */
135 /*
136 * XXX What lock should protect the rsv_goal_size?
137 * Accessed in ext2_get_block only. ext3 uses i_truncate.
138 */
139 mutex_lock(&ei->truncate_mutex);
140 if (!ei->i_block_alloc_info)
141 ext2_init_block_alloc_info(inode);
142
143 if (ei->i_block_alloc_info){
144 struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
145 rsv->rsv_goal_size = rsv_window_size;
146 }
147 mutex_unlock(&ei->truncate_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800148 mnt_drop_write(filp->f_path.mnt);
Martin J. Bligha686cd82007-10-16 23:30:46 -0700149 return 0;
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 default:
152 return -ENOTTY;
153 }
154}
David Howellse322ff02006-08-29 19:06:20 +0100155
156#ifdef CONFIG_COMPAT
157long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
158{
David Howellse322ff02006-08-29 19:06:20 +0100159 /* These are just misnamed, they actually get/put from/to user an int */
160 switch (cmd) {
161 case EXT2_IOC32_GETFLAGS:
162 cmd = EXT2_IOC_GETFLAGS;
163 break;
164 case EXT2_IOC32_SETFLAGS:
165 cmd = EXT2_IOC_SETFLAGS;
166 break;
167 case EXT2_IOC32_GETVERSION:
168 cmd = EXT2_IOC_GETVERSION;
169 break;
170 case EXT2_IOC32_SETVERSION:
171 cmd = EXT2_IOC_SETVERSION;
172 break;
173 default:
174 return -ENOIOCTLCMD;
175 }
Andi Kleen14f9f7b2008-02-06 01:40:10 -0800176 return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
David Howellse322ff02006-08-29 19:06:20 +0100177}
178#endif