blob: 74363a7aacbcb64154c81b1f6bcb832e0fee8727 [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 Torvaldsbd4c6252005-07-12 20:21:28 -070015static int reiserfs_unpack(struct inode *inode, struct file *filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/*
18** reiserfs_ioctl - handler for ioctl for inode
19** supported commands:
20** 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
21** and prevent packing file (argument arg has to be non-zero)
22** 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
23** 3) That's all for a while ...
24*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070025int reiserfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
26 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 unsigned int flags;
Dave Hansen42a74f22008-02-15 14:37:46 -080029 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31 switch (cmd) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070032 case REISERFS_IOC_UNPACK:
33 if (S_ISREG(inode->i_mode)) {
34 if (arg)
35 return reiserfs_unpack(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 else
37 return 0;
38 } else
39 return -ENOTTY;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070040 /* following two cases are taken from fs/ext2/ioctl.c by Remy
41 Card (card@masi.ibp.fr) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 case REISERFS_IOC_GETFLAGS:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070043 if (!reiserfs_attrs(inode->i_sb))
Jeff Mahoney869eb762005-06-29 18:52:28 -040044 return -ENOTTY;
45
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070046 flags = REISERFS_I(inode)->i_attrs;
47 i_attrs_to_sd_attrs(inode, (__u16 *) & flags);
48 return put_user(flags, (int __user *)arg);
49 case REISERFS_IOC_SETFLAGS:{
50 if (!reiserfs_attrs(inode->i_sb))
51 return -ENOTTY;
Jeff Mahoney869eb762005-06-29 18:52:28 -040052
Dave Hansen42a74f22008-02-15 14:37:46 -080053 err = mnt_want_write(filp->f_path.mnt);
54 if (err)
55 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Dave Hansen42a74f22008-02-15 14:37:46 -080057 if (!is_owner_or_cap(inode)) {
58 err = -EPERM;
59 goto setflags_out;
60 }
61 if (get_user(flags, (int __user *)arg)) {
62 err = -EFAULT;
63 goto setflags_out;
64 }
65 /*
66 * Is it quota file? Do not allow user to mess with it
67 */
68 if (IS_NOQUOTA(inode)) {
69 err = -EPERM;
70 goto setflags_out;
71 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070072 if (((flags ^ REISERFS_I(inode)->
73 i_attrs) & (REISERFS_IMMUTABLE_FL |
74 REISERFS_APPEND_FL))
Dave Hansen42a74f22008-02-15 14:37:46 -080075 && !capable(CAP_LINUX_IMMUTABLE)) {
76 err = -EPERM;
77 goto setflags_out;
78 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070079 if ((flags & REISERFS_NOTAIL_FL) &&
80 S_ISREG(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int result;
82
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070083 result = reiserfs_unpack(inode, filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080084 if (result) {
85 err = result;
86 goto setflags_out;
87 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070088 }
89 sd_attrs_to_i_attrs(flags, inode);
90 REISERFS_I(inode)->i_attrs = flags;
91 inode->i_ctime = CURRENT_TIME_SEC;
92 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -080093setflags_out:
94 mnt_drop_write(filp->f_path.mnt);
95 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 case REISERFS_IOC_GETVERSION:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070098 return put_user(inode->i_generation, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 case REISERFS_IOC_SETVERSION:
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530100 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return -EPERM;
Dave Hansen42a74f22008-02-15 14:37:46 -0800102 err = mnt_want_write(filp->f_path.mnt);
103 if (err)
104 return err;
105 if (get_user(inode->i_generation, (int __user *)arg)) {
106 err = -EFAULT;
107 goto setversion_out;
108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 inode->i_ctime = CURRENT_TIME_SEC;
110 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800111setversion_out:
112 mnt_drop_write(filp->f_path.mnt);
113 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 default:
115 return -ENOTTY;
116 }
117}
118
David Howells52b499c2006-08-29 19:06:18 +0100119#ifdef CONFIG_COMPAT
120long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
121 unsigned long arg)
122{
Josef Sipek1fc5adb2006-12-08 02:37:33 -0800123 struct inode *inode = file->f_path.dentry->d_inode;
David Howells52b499c2006-08-29 19:06:18 +0100124 int ret;
125
126 /* These are just misnamed, they actually get/put from/to user an int */
127 switch (cmd) {
128 case REISERFS_IOC32_UNPACK:
129 cmd = REISERFS_IOC_UNPACK;
130 break;
131 case REISERFS_IOC32_GETFLAGS:
132 cmd = REISERFS_IOC_GETFLAGS;
133 break;
134 case REISERFS_IOC32_SETFLAGS:
135 cmd = REISERFS_IOC_SETFLAGS;
136 break;
137 case REISERFS_IOC32_GETVERSION:
138 cmd = REISERFS_IOC_GETVERSION;
139 break;
140 case REISERFS_IOC32_SETVERSION:
141 cmd = REISERFS_IOC_SETVERSION;
142 break;
143 default:
144 return -ENOIOCTLCMD;
145 }
146 lock_kernel();
147 ret = reiserfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
148 unlock_kernel();
149 return ret;
150}
151#endif
152
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700153int reiserfs_commit_write(struct file *f, struct page *page,
154 unsigned from, unsigned to);
155int reiserfs_prepare_write(struct file *f, struct page *page,
156 unsigned from, unsigned to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
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
161*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700162static int reiserfs_unpack(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700164 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700171 if (inode->i_size == 0) {
172 REISERFS_I(inode)->i_flags |= i_nopack_mask;
173 return 0;
174 }
175 /* ioctl already done */
176 if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
177 return 0;
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700180 /* we need to make sure nobody is changing the file size beneath
181 ** us
182 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800183 mutex_lock(&inode->i_mutex);
Chris Masonb5f39532006-08-05 12:15:08 -0700184 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700185
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;
191 }
192
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.
196 */
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;
203 }
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700204 retval = reiserfs_prepare_write(NULL, page, write_from, write_from);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700205 if (retval)
206 goto out_unlock;
207
208 /* conversion can change page contents, must flush */
209 flush_dcache_page(page);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700210 retval = reiserfs_commit_write(NULL, page, write_from, write_from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 REISERFS_I(inode)->i_flags |= i_nopack_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700213 out_unlock:
214 unlock_page(page);
215 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700217 out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800218 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700219 reiserfs_write_unlock(inode->i_sb);
220 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}