blob: 15cb5fe6b425bce7fdd680511e28b0cf4fad3968 [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>
Al Virof466c6f2012-03-17 01:16:43 -04008#include "reiserfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/time.h>
10#include <asm/uaccess.h>
11#include <linux/pagemap.h>
David Howells52b499c2006-08-29 19:06:18 +010012#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/*
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020015 * reiserfs_ioctl - handler for ioctl for inode
16 * supported commands:
17 * 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
18 * and prevent packing file (argument arg has to be non-zero)
19 * 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
20 * 3) That's all for a while ...
21 */
Frederic Weisbecker205cb372009-10-14 23:22:17 +020022long reiserfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Al Viro496ad9a2013-01-23 17:07:38 -050024 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 unsigned int flags;
Dave Hansen42a74f22008-02-15 14:37:46 -080026 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020028 reiserfs_write_lock(inode->i_sb);
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 switch (cmd) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070031 case REISERFS_IOC_UNPACK:
32 if (S_ISREG(inode->i_mode)) {
33 if (arg)
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020034 err = reiserfs_unpack(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 } else
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020036 err = -ENOTTY;
37 break;
38 /*
39 * following two cases are taken from fs/ext2/ioctl.c by Remy
40 * Card (card@masi.ibp.fr)
41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 case REISERFS_IOC_GETFLAGS:
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020043 if (!reiserfs_attrs(inode->i_sb)) {
44 err = -ENOTTY;
45 break;
46 }
Jeff Mahoney869eb762005-06-29 18:52:28 -040047
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070048 flags = REISERFS_I(inode)->i_attrs;
49 i_attrs_to_sd_attrs(inode, (__u16 *) & flags);
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020050 err = put_user(flags, (int __user *)arg);
51 break;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070052 case REISERFS_IOC_SETFLAGS:{
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020053 if (!reiserfs_attrs(inode->i_sb)) {
54 err = -ENOTTY;
55 break;
56 }
Jeff Mahoney869eb762005-06-29 18:52:28 -040057
Al Viroa561be72011-11-23 11:57:51 -050058 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080059 if (err)
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020060 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Serge E. Hallyn2e149672011-03-23 16:43:26 -070062 if (!inode_owner_or_capable(inode)) {
Dave Hansen42a74f22008-02-15 14:37:46 -080063 err = -EPERM;
64 goto setflags_out;
65 }
66 if (get_user(flags, (int __user *)arg)) {
67 err = -EFAULT;
68 goto setflags_out;
69 }
70 /*
71 * Is it quota file? Do not allow user to mess with it
72 */
73 if (IS_NOQUOTA(inode)) {
74 err = -EPERM;
75 goto setflags_out;
76 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070077 if (((flags ^ REISERFS_I(inode)->
78 i_attrs) & (REISERFS_IMMUTABLE_FL |
79 REISERFS_APPEND_FL))
Dave Hansen42a74f22008-02-15 14:37:46 -080080 && !capable(CAP_LINUX_IMMUTABLE)) {
81 err = -EPERM;
82 goto setflags_out;
83 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070084 if ((flags & REISERFS_NOTAIL_FL) &&
85 S_ISREG(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 int result;
87
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070088 result = reiserfs_unpack(inode, filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080089 if (result) {
90 err = result;
91 goto setflags_out;
92 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070093 }
94 sd_attrs_to_i_attrs(flags, inode);
95 REISERFS_I(inode)->i_attrs = flags;
96 inode->i_ctime = CURRENT_TIME_SEC;
97 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -080098setflags_out:
Al Viro2a79f172011-12-09 08:06:57 -050099 mnt_drop_write_file(filp);
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200100 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 case REISERFS_IOC_GETVERSION:
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200103 err = put_user(inode->i_generation, (int __user *)arg);
104 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 case REISERFS_IOC_SETVERSION:
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700106 if (!inode_owner_or_capable(inode)) {
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200107 err = -EPERM;
108 break;
Jiri Slabye0baec12010-01-06 23:09:50 +0100109 }
Al Viroa561be72011-11-23 11:57:51 -0500110 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800111 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:
Al Viro2a79f172011-12-09 08:06:57 -0500120 mnt_drop_write_file(filp);
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/*
163** reiserfs_unpack
164** Function try to convert tail from direct item into indirect.
165** It set up nopack attribute in the REISERFS_I(inode)->nopack
166*/
Jan Karad5dee5c2008-04-28 02:16:23 -0700167int reiserfs_unpack(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700169 int retval = 0;
Frederic Weisbecker9d8117e2010-09-30 15:15:38 -0700170 int depth;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700171 int index;
172 struct page *page;
173 struct address_space *mapping;
174 unsigned long write_from;
175 unsigned long blocksize = inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700177 if (inode->i_size == 0) {
178 REISERFS_I(inode)->i_flags |= i_nopack_mask;
179 return 0;
180 }
181 /* ioctl already done */
182 if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
183 return 0;
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Frederic Weisbecker9d8117e2010-09-30 15:15:38 -0700186 depth = reiserfs_write_lock_once(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700187
Frederic Weisbeckerda905872010-11-24 12:57:15 -0800188 /* we need to make sure nobody is changing the file size beneath us */
189 reiserfs_mutex_lock_safe(&inode->i_mutex, inode->i_sb);
190
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700191 write_from = inode->i_size & (blocksize - 1);
192 /* if we are on a block boundary, we are already unpacked. */
193 if (write_from == 0) {
194 REISERFS_I(inode)->i_flags |= i_nopack_mask;
195 goto out;
196 }
197
198 /* we unpack by finding the page with the tail, and calling
Christoph Hellwigebdec242010-10-06 10:47:23 +0200199 ** __reiserfs_write_begin on that page. This will force a
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700200 ** reiserfs_get_block to unpack the tail for us.
201 */
202 index = inode->i_size >> PAGE_CACHE_SHIFT;
203 mapping = inode->i_mapping;
204 page = grab_cache_page(mapping, index);
205 retval = -ENOMEM;
206 if (!page) {
207 goto out;
208 }
Christoph Hellwigebdec242010-10-06 10:47:23 +0200209 retval = __reiserfs_write_begin(page, write_from, 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700210 if (retval)
211 goto out_unlock;
212
213 /* conversion can change page contents, must flush */
214 flush_dcache_page(page);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700215 retval = reiserfs_commit_write(NULL, page, write_from, write_from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 REISERFS_I(inode)->i_flags |= i_nopack_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700218 out_unlock:
219 unlock_page(page);
220 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700222 out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800223 mutex_unlock(&inode->i_mutex);
Frederic Weisbecker9d8117e2010-09-30 15:15:38 -0700224 reiserfs_write_unlock_once(inode->i_sb, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700225 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}