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