Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[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/mount.h>
15 #include <asm/current.h>
16 #include <asm/uaccess.h>
17
18
19 int ext2_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
20                 unsigned long arg)
21 {
22         struct ext2_inode_info *ei = EXT2_I(inode);
23         unsigned int flags;
24
25         ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
26
27         switch (cmd) {
28         case EXT2_IOC_GETFLAGS:
29                 flags = ei->i_flags & EXT2_FL_USER_VISIBLE;
30                 return put_user(flags, (int __user *) arg);
31         case EXT2_IOC_SETFLAGS: {
32                 unsigned int oldflags;
33
34                 if (IS_RDONLY(inode) ||
35                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
36                         return -EROFS;
37
38                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
39                         return -EACCES;
40
41                 if (get_user(flags, (int __user *) arg))
42                         return -EFAULT;
43
44                 if (!S_ISDIR(inode->i_mode))
45                         flags &= ~EXT2_DIRSYNC_FL;
46
47                 oldflags = ei->i_flags;
48
49                 /*
50                  * The IMMUTABLE and APPEND_ONLY flags can only be changed by
51                  * the relevant capability.
52                  *
53                  * This test looks nicer. Thanks to Pauline Middelink
54                  */
55                 if ((oldflags & EXT2_IMMUTABLE_FL) ||
56                         ((flags ^ oldflags) & (EXT2_APPEND_FL |
57                         EXT2_IMMUTABLE_FL | EXT2_IUNLINK_FL))) {
58                         if (!capable(CAP_LINUX_IMMUTABLE))
59                                 return -EPERM;
60                 }
61
62                 flags = flags & EXT2_FL_USER_MODIFIABLE;
63                 flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
64                 ei->i_flags = flags;
65
66                 ext2_set_inode_flags(inode);
67                 inode->i_ctime = CURRENT_TIME_SEC;
68                 mark_inode_dirty(inode);
69                 return 0;
70         }
71         case EXT2_IOC_GETVERSION:
72                 return put_user(inode->i_generation, (int __user *) arg);
73         case EXT2_IOC_SETVERSION:
74                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
75                         return -EPERM;
76                 if (IS_RDONLY(inode) ||
77                         (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
78                         return -EROFS;
79                 if (get_user(inode->i_generation, (int __user *) arg))
80                         return -EFAULT; 
81                 inode->i_ctime = CURRENT_TIME_SEC;
82                 mark_inode_dirty(inode);
83                 return 0;
84         default:
85                 return -ENOTTY;
86         }
87 }