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