fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / ext2 / ioctl.c
1 /*
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"
11 #include <linux/capability.h>
12 #include <linux/time.h>
13 #include <linux/sched.h>
14 #include <linux/compat.h>
15 #include <linux/smp_lock.h>
16 #include <linux/mount.h>
17 #include <asm/current.h>
18 #include <asm/uaccess.h>
19
20
21 int ext2_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
22                 unsigned long arg)
23 {
24         struct ext2_inode_info *ei = EXT2_I(inode);
25         unsigned int flags;
26
27         ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
28
29         switch (cmd) {
30         case EXT2_IOC_GETFLAGS:
31                 flags = ei->i_flags & EXT2_FL_USER_VISIBLE;
32                 return put_user(flags, (int __user *) arg);
33         case EXT2_IOC_SETFLAGS: {
34                 unsigned int oldflags;
35
36                 if (IS_RDONLY(inode) ||
37                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
38                         return -EROFS;
39
40                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
41                         return -EACCES;
42
43                 if (get_user(flags, (int __user *) arg))
44                         return -EFAULT;
45
46                 if (!S_ISDIR(inode->i_mode))
47                         flags &= ~EXT2_DIRSYNC_FL;
48
49                 mutex_lock(&inode->i_mutex);
50                 oldflags = ei->i_flags;
51
52                 /*
53                  * The IMMUTABLE and APPEND_ONLY flags can only be changed by
54                  * the relevant capability.
55                  *
56                  * This test looks nicer. Thanks to Pauline Middelink
57                  */
58                 if ((oldflags & EXT2_IMMUTABLE_FL) ||
59                         ((flags ^ oldflags) & (EXT2_APPEND_FL |
60                         EXT2_IMMUTABLE_FL | EXT2_IUNLINK_FL))) {
61                         if (!capable(CAP_LINUX_IMMUTABLE)) {
62                                 mutex_unlock(&inode->i_mutex);
63                                 return -EPERM;
64                         }
65                 }
66
67                 flags = flags & EXT2_FL_USER_MODIFIABLE;
68                 flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
69                 ei->i_flags = flags;
70                 mutex_unlock(&inode->i_mutex);
71
72                 ext2_set_inode_flags(inode);
73                 inode->i_ctime = CURRENT_TIME_SEC;
74                 mark_inode_dirty(inode);
75                 return 0;
76         }
77         case EXT2_IOC_GETVERSION:
78                 return put_user(inode->i_generation, (int __user *) arg);
79         case EXT2_IOC_SETVERSION:
80                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
81                         return -EPERM;
82                 if (IS_RDONLY(inode) ||
83                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
84                         return -EROFS;
85                 if (get_user(inode->i_generation, (int __user *) arg))
86                         return -EFAULT; 
87                 inode->i_ctime = CURRENT_TIME_SEC;
88                 mark_inode_dirty(inode);
89                 return 0;
90         default:
91                 return -ENOTTY;
92         }
93 }
94
95 #ifdef CONFIG_COMPAT
96 long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
97 {
98         struct inode *inode = file->f_path.dentry->d_inode;
99         int ret;
100
101         /* These are just misnamed, they actually get/put from/to user an int */
102         switch (cmd) {
103         case EXT2_IOC32_GETFLAGS:
104                 cmd = EXT2_IOC_GETFLAGS;
105                 break;
106         case EXT2_IOC32_SETFLAGS:
107                 cmd = EXT2_IOC_SETFLAGS;
108                 break;
109         case EXT2_IOC32_GETVERSION:
110                 cmd = EXT2_IOC_GETVERSION;
111                 break;
112         case EXT2_IOC32_SETVERSION:
113                 cmd = EXT2_IOC_SETVERSION;
114                 break;
115         default:
116                 return -ENOIOCTLCMD;
117         }
118         lock_kernel();
119         ret = ext2_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
120         unlock_kernel();
121         return ret;
122 }
123 #endif