patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / ext3 / ioctl.c
1 /*
2  * linux/fs/ext3/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 <linux/fs.h>
11 #include <linux/jbd.h>
12 #include <linux/ext3_fs.h>
13 #include <linux/ext3_jbd.h>
14 #include <linux/time.h>
15 #include <linux/vserver/xid.h>
16 #include <asm/uaccess.h>
17
18
19 int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
20                 unsigned long arg)
21 {
22         struct ext3_inode_info *ei = EXT3_I(inode);
23         unsigned int flags;
24
25         ext3_debug ("cmd = %u, arg = %lu\n", cmd, arg);
26
27         switch (cmd) {
28         case EXT3_IOC_GETFLAGS:
29                 flags = ei->i_flags & EXT3_FL_USER_VISIBLE;
30                 return put_user(flags, (int __user *) arg);
31         case EXT3_IOC_SETFLAGS: {
32                 handle_t *handle = NULL;
33                 int err;
34                 struct ext3_iloc iloc;
35                 unsigned int oldflags;
36                 unsigned int jflag;
37
38                 if (IS_RDONLY(inode))
39                         return -EROFS;
40
41                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
42                         return -EACCES;
43
44                 if (get_user(flags, (int __user *) arg))
45                         return -EFAULT;
46
47                 if (!S_ISDIR(inode->i_mode))
48                         flags &= ~EXT3_DIRSYNC_FL;
49
50                 oldflags = ei->i_flags;
51
52                 /* The JOURNAL_DATA flag is modifiable only by root */
53                 jflag = flags & EXT3_JOURNAL_DATA_FL;
54
55                 /*
56                  * The IMMUTABLE and APPEND_ONLY flags can only be changed by
57                  * the relevant capability.
58                  *
59                  * This test looks nicer. Thanks to Pauline Middelink
60                  */
61                 if ((oldflags & EXT3_IMMUTABLE_FL) ||
62                         ((flags ^ oldflags) &
63                         (EXT3_APPEND_FL | EXT3_IMMUTABLE_FL))) {
64                         if (!capable(CAP_LINUX_IMMUTABLE))
65                                 return -EPERM;
66                 }
67
68                 /*
69                  * The JOURNAL_DATA flag can only be changed by
70                  * the relevant capability.
71                  */
72                 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
73                         if (!capable(CAP_SYS_RESOURCE))
74                                 return -EPERM;
75                 }
76
77
78                 handle = ext3_journal_start(inode, 1);
79                 if (IS_ERR(handle))
80                         return PTR_ERR(handle);
81                 if (IS_SYNC(inode))
82                         handle->h_sync = 1;
83                 err = ext3_reserve_inode_write(handle, inode, &iloc);
84                 if (err)
85                         goto flags_err;
86
87                 flags = flags & EXT3_FL_USER_MODIFIABLE;
88                 flags |= oldflags & ~EXT3_FL_USER_MODIFIABLE;
89                 ei->i_flags = flags;
90
91                 ext3_set_inode_flags(inode);
92                 inode->i_ctime = CURRENT_TIME;
93
94                 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
95 flags_err:
96                 ext3_journal_stop(handle);
97                 if (err)
98                         return err;
99
100                 if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
101                         err = ext3_change_inode_journal_flag(inode, jflag);
102                 return err;
103         }
104         case EXT3_IOC_GETVERSION:
105         case EXT3_IOC_GETVERSION_OLD:
106                 return put_user(inode->i_generation, (int __user *) arg);
107         case EXT3_IOC_SETVERSION:
108         case EXT3_IOC_SETVERSION_OLD: {
109                 handle_t *handle;
110                 struct ext3_iloc iloc;
111                 __u32 generation;
112                 int err;
113
114                 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
115                         return -EPERM;
116                 if (IS_RDONLY(inode))
117                         return -EROFS;
118                 if (get_user(generation, (int __user *) arg))
119                         return -EFAULT;
120
121                 handle = ext3_journal_start(inode, 1);
122                 if (IS_ERR(handle))
123                         return PTR_ERR(handle);
124                 err = ext3_reserve_inode_write(handle, inode, &iloc);
125                 if (err == 0) {
126                         inode->i_ctime = CURRENT_TIME;
127                         inode->i_generation = generation;
128                         err = ext3_mark_iloc_dirty(handle, inode, &iloc);
129                 }
130                 ext3_journal_stop(handle);
131                 return err;
132         }
133 #ifdef CONFIG_JBD_DEBUG
134         case EXT3_IOC_WAIT_FOR_READONLY:
135                 /*
136                  * This is racy - by the time we're woken up and running,
137                  * the superblock could be released.  And the module could
138                  * have been unloaded.  So sue me.
139                  *
140                  * Returns 1 if it slept, else zero.
141                  */
142                 {
143                         struct super_block *sb = inode->i_sb;
144                         DECLARE_WAITQUEUE(wait, current);
145                         int ret = 0;
146
147                         set_current_state(TASK_INTERRUPTIBLE);
148                         add_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
149                         if (timer_pending(&EXT3_SB(sb)->turn_ro_timer)) {
150                                 schedule();
151                                 ret = 1;
152                         }
153                         remove_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
154                         return ret;
155                 }
156 #endif
157 #if defined(CONFIG_VSERVER_LEGACY) && !defined(CONFIG_INOXID_NONE)
158         case EXT3_IOC_SETXID: {
159                 handle_t *handle;
160                 struct ext3_iloc iloc;
161                 int xid;
162                 int err;
163
164                 /* fixme: if stealth, return -ENOTTY */
165                 if (!capable(CAP_CONTEXT))
166                         return -EPERM;
167                 if (IS_RDONLY(inode))
168                         return -EROFS;
169                 if (!(inode->i_sb->s_flags & MS_TAGXID))
170                         return -ENOSYS;
171                 if (get_user(xid, (int *) arg))
172                         return -EFAULT; 
173
174                 handle = ext3_journal_start(inode, 1);
175                 if (IS_ERR(handle))
176                         return PTR_ERR(handle);
177                 err = ext3_reserve_inode_write(handle, inode, &iloc);
178                 if (err)
179                         return err;
180
181                 inode->i_xid = (xid & 0xFFFF);
182                 inode->i_ctime = CURRENT_TIME;
183
184                 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
185                 ext3_journal_stop(handle);
186                 return err;
187         }
188 #endif
189         default:
190                 return -ENOTTY;
191         }
192 }