blob: ace77451ceb16d3280a94e4bebc1044e0c6920f3 [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>
Dave Hansen42a74f22008-02-15 14:37:46 -08007#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#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>
David Howells52b499c2006-08-29 19:06:18 +010013#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Linus Torvalds1da177e2005-04-16 15:20:36 -070015/*
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020016 * 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 ...
22 */
Frederic Weisbecker205cb372009-10-14 23:22:17 +020023long reiserfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Frederic Weisbecker205cb372009-10-14 23:22:17 +020025 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 unsigned int flags;
Dave Hansen42a74f22008-02-15 14:37:46 -080027 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020029 reiserfs_write_lock(inode->i_sb);
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 switch (cmd) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070032 case REISERFS_IOC_UNPACK:
33 if (S_ISREG(inode->i_mode)) {
34 if (arg)
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020035 err = reiserfs_unpack(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 } else
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020037 err = -ENOTTY;
38 break;
39 /*
40 * following two cases are taken from fs/ext2/ioctl.c by Remy
41 * Card (card@masi.ibp.fr)
42 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 case REISERFS_IOC_GETFLAGS:
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020044 if (!reiserfs_attrs(inode->i_sb)) {
45 err = -ENOTTY;
46 break;
47 }
Jeff Mahoney869eb762005-06-29 18:52:28 -040048
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070049 flags = REISERFS_I(inode)->i_attrs;
50 i_attrs_to_sd_attrs(inode, (__u16 *) & flags);
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020051 err = put_user(flags, (int __user *)arg);
52 break;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070053 case REISERFS_IOC_SETFLAGS:{
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020054 if (!reiserfs_attrs(inode->i_sb)) {
55 err = -ENOTTY;
56 break;
57 }
Jeff Mahoney869eb762005-06-29 18:52:28 -040058
Dave Hansen42a74f22008-02-15 14:37:46 -080059 err = mnt_want_write(filp->f_path.mnt);
60 if (err)
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020061 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Dave Hansen42a74f22008-02-15 14:37:46 -080063 if (!is_owner_or_cap(inode)) {
64 err = -EPERM;
65 goto setflags_out;
66 }
67 if (get_user(flags, (int __user *)arg)) {
68 err = -EFAULT;
69 goto setflags_out;
70 }
71 /*
72 * Is it quota file? Do not allow user to mess with it
73 */
74 if (IS_NOQUOTA(inode)) {
75 err = -EPERM;
76 goto setflags_out;
77 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070078 if (((flags ^ REISERFS_I(inode)->
79 i_attrs) & (REISERFS_IMMUTABLE_FL |
80 REISERFS_APPEND_FL))
Dave Hansen42a74f22008-02-15 14:37:46 -080081 && !capable(CAP_LINUX_IMMUTABLE)) {
82 err = -EPERM;
83 goto setflags_out;
84 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070085 if ((flags & REISERFS_NOTAIL_FL) &&
86 S_ISREG(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 int result;
88
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070089 result = reiserfs_unpack(inode, filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080090 if (result) {
91 err = result;
92 goto setflags_out;
93 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070094 }
95 sd_attrs_to_i_attrs(flags, inode);
96 REISERFS_I(inode)->i_attrs = flags;
97 inode->i_ctime = CURRENT_TIME_SEC;
98 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -080099setflags_out:
100 mnt_drop_write(filp->f_path.mnt);
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 case REISERFS_IOC_GETVERSION:
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200104 err = put_user(inode->i_generation, (int __user *)arg);
105 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 case REISERFS_IOC_SETVERSION:
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530107 if (!is_owner_or_cap(inode))
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200108 err = -EPERM;
109 break;
Dave Hansen42a74f22008-02-15 14:37:46 -0800110 err = mnt_want_write(filp->f_path.mnt);
111 if (err)
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200112 break;
Dave Hansen42a74f22008-02-15 14:37:46 -0800113 if (get_user(inode->i_generation, (int __user *)arg)) {
114 err = -EFAULT;
115 goto setversion_out;
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 inode->i_ctime = CURRENT_TIME_SEC;
118 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800119setversion_out:
120 mnt_drop_write(filp->f_path.mnt);
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200121 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 default:
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200123 err = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200125
126 reiserfs_write_unlock(inode->i_sb);
127
128 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
David Howells52b499c2006-08-29 19:06:18 +0100131#ifdef CONFIG_COMPAT
132long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
133 unsigned long arg)
134{
David Howells52b499c2006-08-29 19:06:18 +0100135 /* These are just misnamed, they actually get/put from/to user an int */
136 switch (cmd) {
137 case REISERFS_IOC32_UNPACK:
138 cmd = REISERFS_IOC_UNPACK;
139 break;
140 case REISERFS_IOC32_GETFLAGS:
141 cmd = REISERFS_IOC_GETFLAGS;
142 break;
143 case REISERFS_IOC32_SETFLAGS:
144 cmd = REISERFS_IOC_SETFLAGS;
145 break;
146 case REISERFS_IOC32_GETVERSION:
147 cmd = REISERFS_IOC_GETVERSION;
148 break;
149 case REISERFS_IOC32_SETVERSION:
150 cmd = REISERFS_IOC_SETVERSION;
151 break;
152 default:
153 return -ENOIOCTLCMD;
154 }
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200155
Frederic Weisbecker205cb372009-10-14 23:22:17 +0200156 return reiserfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
David Howells52b499c2006-08-29 19:06:18 +0100157}
158#endif
159
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700160int reiserfs_commit_write(struct file *f, struct page *page,
161 unsigned from, unsigned to);
162int reiserfs_prepare_write(struct file *f, struct page *page,
163 unsigned from, unsigned to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
165** reiserfs_unpack
166** Function try to convert tail from direct item into indirect.
167** It set up nopack attribute in the REISERFS_I(inode)->nopack
168*/
Jan Karad5dee5c2008-04-28 02:16:23 -0700169int reiserfs_unpack(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700171 int retval = 0;
172 int index;
173 struct page *page;
174 struct address_space *mapping;
175 unsigned long write_from;
176 unsigned long blocksize = inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700178 if (inode->i_size == 0) {
179 REISERFS_I(inode)->i_flags |= i_nopack_mask;
180 return 0;
181 }
182 /* ioctl already done */
183 if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
184 return 0;
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700187 /* we need to make sure nobody is changing the file size beneath
188 ** us
189 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800190 mutex_lock(&inode->i_mutex);
Chris Masonb5f39532006-08-05 12:15:08 -0700191 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700192
193 write_from = inode->i_size & (blocksize - 1);
194 /* if we are on a block boundary, we are already unpacked. */
195 if (write_from == 0) {
196 REISERFS_I(inode)->i_flags |= i_nopack_mask;
197 goto out;
198 }
199
200 /* we unpack by finding the page with the tail, and calling
Jeff Mahoney0222e652009-03-30 14:02:44 -0400201 ** reiserfs_prepare_write on that page. This will force a
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700202 ** reiserfs_get_block to unpack the tail for us.
203 */
204 index = inode->i_size >> PAGE_CACHE_SHIFT;
205 mapping = inode->i_mapping;
206 page = grab_cache_page(mapping, index);
207 retval = -ENOMEM;
208 if (!page) {
209 goto out;
210 }
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700211 retval = reiserfs_prepare_write(NULL, page, write_from, write_from);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700212 if (retval)
213 goto out_unlock;
214
215 /* conversion can change page contents, must flush */
216 flush_dcache_page(page);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700217 retval = reiserfs_commit_write(NULL, page, write_from, write_from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 REISERFS_I(inode)->i_flags |= i_nopack_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700220 out_unlock:
221 unlock_page(page);
222 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700224 out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800225 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700226 reiserfs_write_unlock(inode->i_sb);
227 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}