fed up with those stupid warnings
[mmotm.git] / fs / reiserfs / ioctl.c
blob5e40b0cd4c3d703f57fc96061f305d6e84491a7c
1 /*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
5 #include <linux/capability.h>
6 #include <linux/fs.h>
7 #include <linux/mount.h>
8 #include <linux/reiserfs_fs.h>
9 #include <linux/time.h>
10 #include <asm/uaccess.h>
11 #include <linux/pagemap.h>
12 #include <linux/smp_lock.h>
13 #include <linux/compat.h>
16 ** reiserfs_ioctl - handler for ioctl for inode
17 ** supported commands:
18 ** 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
19 ** and prevent packing file (argument arg has to be non-zero)
20 ** 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
21 ** 3) That's all for a while ...
23 int reiserfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
24 unsigned long arg)
26 unsigned int flags;
27 int err = 0;
29 switch (cmd) {
30 case REISERFS_IOC_UNPACK:
31 if (S_ISREG(inode->i_mode)) {
32 if (arg)
33 return reiserfs_unpack(inode, filp);
34 else
35 return 0;
36 } else
37 return -ENOTTY;
38 /* following two cases are taken from fs/ext2/ioctl.c by Remy
39 Card (card@masi.ibp.fr) */
40 case REISERFS_IOC_GETFLAGS:
41 if (!reiserfs_attrs(inode->i_sb))
42 return -ENOTTY;
44 flags = REISERFS_I(inode)->i_attrs;
45 i_attrs_to_sd_attrs(inode, (__u16 *) & flags);
46 return put_user(flags, (int __user *)arg);
47 case REISERFS_IOC_SETFLAGS:{
48 if (!reiserfs_attrs(inode->i_sb))
49 return -ENOTTY;
51 err = mnt_want_write(filp->f_path.mnt);
52 if (err)
53 return err;
55 if (!is_owner_or_cap(inode)) {
56 err = -EPERM;
57 goto setflags_out;
59 if (get_user(flags, (int __user *)arg)) {
60 err = -EFAULT;
61 goto setflags_out;
64 * Is it quota file? Do not allow user to mess with it
66 if (IS_NOQUOTA(inode)) {
67 err = -EPERM;
68 goto setflags_out;
70 if (((flags ^ REISERFS_I(inode)->
71 i_attrs) & (REISERFS_IMMUTABLE_FL |
72 REISERFS_APPEND_FL))
73 && !capable(CAP_LINUX_IMMUTABLE)) {
74 err = -EPERM;
75 goto setflags_out;
77 if ((flags & REISERFS_NOTAIL_FL) &&
78 S_ISREG(inode->i_mode)) {
79 int result;
81 result = reiserfs_unpack(inode, filp);
82 if (result) {
83 err = result;
84 goto setflags_out;
87 sd_attrs_to_i_attrs(flags, inode);
88 REISERFS_I(inode)->i_attrs = flags;
89 inode->i_ctime = CURRENT_TIME_SEC;
90 mark_inode_dirty(inode);
91 setflags_out:
92 mnt_drop_write(filp->f_path.mnt);
93 return err;
95 case REISERFS_IOC_GETVERSION:
96 return put_user(inode->i_generation, (int __user *)arg);
97 case REISERFS_IOC_SETVERSION:
98 if (!is_owner_or_cap(inode))
99 return -EPERM;
100 err = mnt_want_write(filp->f_path.mnt);
101 if (err)
102 return err;
103 if (get_user(inode->i_generation, (int __user *)arg)) {
104 err = -EFAULT;
105 goto setversion_out;
107 inode->i_ctime = CURRENT_TIME_SEC;
108 mark_inode_dirty(inode);
109 setversion_out:
110 mnt_drop_write(filp->f_path.mnt);
111 return err;
112 default:
113 return -ENOTTY;
117 #ifdef CONFIG_COMPAT
118 long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
119 unsigned long arg)
121 struct inode *inode = file->f_path.dentry->d_inode;
122 int ret;
124 /* These are just misnamed, they actually get/put from/to user an int */
125 switch (cmd) {
126 case REISERFS_IOC32_UNPACK:
127 cmd = REISERFS_IOC_UNPACK;
128 break;
129 case REISERFS_IOC32_GETFLAGS:
130 cmd = REISERFS_IOC_GETFLAGS;
131 break;
132 case REISERFS_IOC32_SETFLAGS:
133 cmd = REISERFS_IOC_SETFLAGS;
134 break;
135 case REISERFS_IOC32_GETVERSION:
136 cmd = REISERFS_IOC_GETVERSION;
137 break;
138 case REISERFS_IOC32_SETVERSION:
139 cmd = REISERFS_IOC_SETVERSION;
140 break;
141 default:
142 return -ENOIOCTLCMD;
145 reiserfs_write_lock(inode->i_sb);
146 ret = reiserfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
147 reiserfs_write_unlock(inode->i_sb);
149 return ret;
151 #endif
153 int reiserfs_commit_write(struct file *f, struct page *page,
154 unsigned from, unsigned to);
155 int reiserfs_prepare_write(struct file *f, struct page *page,
156 unsigned from, unsigned to);
158 ** reiserfs_unpack
159 ** Function try to convert tail from direct item into indirect.
160 ** It set up nopack attribute in the REISERFS_I(inode)->nopack
162 int reiserfs_unpack(struct inode *inode, struct file *filp)
164 int retval = 0;
165 int index;
166 struct page *page;
167 struct address_space *mapping;
168 unsigned long write_from;
169 unsigned long blocksize = inode->i_sb->s_blocksize;
171 if (inode->i_size == 0) {
172 REISERFS_I(inode)->i_flags |= i_nopack_mask;
173 return 0;
175 /* ioctl already done */
176 if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
177 return 0;
180 /* we need to make sure nobody is changing the file size beneath
181 ** us
183 mutex_lock(&inode->i_mutex);
184 reiserfs_write_lock(inode->i_sb);
186 write_from = inode->i_size & (blocksize - 1);
187 /* if we are on a block boundary, we are already unpacked. */
188 if (write_from == 0) {
189 REISERFS_I(inode)->i_flags |= i_nopack_mask;
190 goto out;
193 /* we unpack by finding the page with the tail, and calling
194 ** reiserfs_prepare_write on that page. This will force a
195 ** reiserfs_get_block to unpack the tail for us.
197 index = inode->i_size >> PAGE_CACHE_SHIFT;
198 mapping = inode->i_mapping;
199 page = grab_cache_page(mapping, index);
200 retval = -ENOMEM;
201 if (!page) {
202 goto out;
204 retval = reiserfs_prepare_write(NULL, page, write_from, write_from);
205 if (retval)
206 goto out_unlock;
208 /* conversion can change page contents, must flush */
209 flush_dcache_page(page);
210 retval = reiserfs_commit_write(NULL, page, write_from, write_from);
211 REISERFS_I(inode)->i_flags |= i_nopack_mask;
213 out_unlock:
214 unlock_page(page);
215 page_cache_release(page);
217 out:
218 mutex_unlock(&inode->i_mutex);
219 reiserfs_write_unlock(inode->i_sb);
220 return retval;