blob: e0f0f098a523a8d1c3a41099ab76b411989a23a0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
Randy Dunlap16f7e0f2006-01-11 12:17:46 -08005#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/fs.h>
7#include <linux/reiserfs_fs.h>
8#include <linux/time.h>
9#include <asm/uaccess.h>
10#include <linux/pagemap.h>
11#include <linux/smp_lock.h>
David Howells52b499c2006-08-29 19:06:18 +010012#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070014static int reiserfs_unpack(struct inode *inode, struct file *filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/*
17** reiserfs_ioctl - handler for ioctl for inode
18** supported commands:
19** 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
20** and prevent packing file (argument arg has to be non-zero)
21** 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
22** 3) That's all for a while ...
23*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070024int reiserfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
25 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
27 unsigned int flags;
28
29 switch (cmd) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070030 case REISERFS_IOC_UNPACK:
31 if (S_ISREG(inode->i_mode)) {
32 if (arg)
33 return reiserfs_unpack(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 else
35 return 0;
36 } else
37 return -ENOTTY;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070038 /* following two cases are taken from fs/ext2/ioctl.c by Remy
39 Card (card@masi.ibp.fr) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 case REISERFS_IOC_GETFLAGS:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070041 if (!reiserfs_attrs(inode->i_sb))
Jeff Mahoney869eb762005-06-29 18:52:28 -040042 return -ENOTTY;
43
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070044 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;
Jeff Mahoney869eb762005-06-29 18:52:28 -040050
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070051 if (IS_RDONLY(inode))
52 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Satyam Sharma3bd858a2007-07-17 15:00:08 +053054 if (!is_owner_or_cap(inode))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070055 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070057 if (get_user(flags, (int __user *)arg))
58 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jan Karae47776a2007-11-14 16:58:56 -080060 /* Is it quota file? Do not allow user to mess with it. */
61 if (IS_NOQUOTA(inode))
62 return -EPERM;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070063 if (((flags ^ REISERFS_I(inode)->
64 i_attrs) & (REISERFS_IMMUTABLE_FL |
65 REISERFS_APPEND_FL))
66 && !capable(CAP_LINUX_IMMUTABLE))
67 return -EPERM;
68
69 if ((flags & REISERFS_NOTAIL_FL) &&
70 S_ISREG(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 int result;
72
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070073 result = reiserfs_unpack(inode, filp);
74 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return result;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070076 }
77 sd_attrs_to_i_attrs(flags, inode);
78 REISERFS_I(inode)->i_attrs = flags;
79 inode->i_ctime = CURRENT_TIME_SEC;
80 mark_inode_dirty(inode);
81 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 case REISERFS_IOC_GETVERSION:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070084 return put_user(inode->i_generation, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 case REISERFS_IOC_SETVERSION:
Satyam Sharma3bd858a2007-07-17 15:00:08 +053086 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return -EPERM;
88 if (IS_RDONLY(inode))
89 return -EROFS;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070090 if (get_user(inode->i_generation, (int __user *)arg))
91 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 inode->i_ctime = CURRENT_TIME_SEC;
93 mark_inode_dirty(inode);
94 return 0;
95 default:
96 return -ENOTTY;
97 }
98}
99
David Howells52b499c2006-08-29 19:06:18 +0100100#ifdef CONFIG_COMPAT
101long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
102 unsigned long arg)
103{
Josef Sipek1fc5adb2006-12-08 02:37:33 -0800104 struct inode *inode = file->f_path.dentry->d_inode;
David Howells52b499c2006-08-29 19:06:18 +0100105 int ret;
106
107 /* These are just misnamed, they actually get/put from/to user an int */
108 switch (cmd) {
109 case REISERFS_IOC32_UNPACK:
110 cmd = REISERFS_IOC_UNPACK;
111 break;
112 case REISERFS_IOC32_GETFLAGS:
113 cmd = REISERFS_IOC_GETFLAGS;
114 break;
115 case REISERFS_IOC32_SETFLAGS:
116 cmd = REISERFS_IOC_SETFLAGS;
117 break;
118 case REISERFS_IOC32_GETVERSION:
119 cmd = REISERFS_IOC_GETVERSION;
120 break;
121 case REISERFS_IOC32_SETVERSION:
122 cmd = REISERFS_IOC_SETVERSION;
123 break;
124 default:
125 return -ENOIOCTLCMD;
126 }
127 lock_kernel();
128 ret = reiserfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
129 unlock_kernel();
130 return ret;
131}
132#endif
133
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700134int reiserfs_commit_write(struct file *f, struct page *page,
135 unsigned from, unsigned to);
136int reiserfs_prepare_write(struct file *f, struct page *page,
137 unsigned from, unsigned to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139** reiserfs_unpack
140** Function try to convert tail from direct item into indirect.
141** It set up nopack attribute in the REISERFS_I(inode)->nopack
142*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143static int reiserfs_unpack(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700145 int retval = 0;
146 int index;
147 struct page *page;
148 struct address_space *mapping;
149 unsigned long write_from;
150 unsigned long blocksize = inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700152 if (inode->i_size == 0) {
153 REISERFS_I(inode)->i_flags |= i_nopack_mask;
154 return 0;
155 }
156 /* ioctl already done */
157 if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
158 return 0;
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700161 /* we need to make sure nobody is changing the file size beneath
162 ** us
163 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800164 mutex_lock(&inode->i_mutex);
Chris Masonb5f39532006-08-05 12:15:08 -0700165 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700166
167 write_from = inode->i_size & (blocksize - 1);
168 /* if we are on a block boundary, we are already unpacked. */
169 if (write_from == 0) {
170 REISERFS_I(inode)->i_flags |= i_nopack_mask;
171 goto out;
172 }
173
174 /* we unpack by finding the page with the tail, and calling
175 ** reiserfs_prepare_write on that page. This will force a
176 ** reiserfs_get_block to unpack the tail for us.
177 */
178 index = inode->i_size >> PAGE_CACHE_SHIFT;
179 mapping = inode->i_mapping;
180 page = grab_cache_page(mapping, index);
181 retval = -ENOMEM;
182 if (!page) {
183 goto out;
184 }
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700185 retval = reiserfs_prepare_write(NULL, page, write_from, write_from);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700186 if (retval)
187 goto out_unlock;
188
189 /* conversion can change page contents, must flush */
190 flush_dcache_page(page);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700191 retval = reiserfs_commit_write(NULL, page, write_from, write_from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 REISERFS_I(inode)->i_flags |= i_nopack_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194 out_unlock:
195 unlock_page(page);
196 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700198 out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800199 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700200 reiserfs_write_unlock(inode->i_sb);
201 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}