blob: adb21bea3d600e7f079d4868d44279c39904c99f [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>
Fabian Frederick170939912014-08-08 14:21:12 -070010#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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
Jeff Mahoney098297b2014-04-23 10:00:36 -040018 * and prevent packing file (argument arg has t
19 * be non-zero)
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020020 * 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{
Al Viro496ad9a2013-01-23 17:07:38 -050025 struct inode *inode = file_inode(filp);
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;
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 }
Darrick J. Wong5aca2842019-07-01 08:25:34 -070077 err = vfs_ioc_setflags_prepare(inode,
78 REISERFS_I(inode)->i_attrs,
79 flags);
80 if (err)
Dave Hansen42a74f22008-02-15 14:37:46 -080081 goto setflags_out;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070082 if ((flags & REISERFS_NOTAIL_FL) &&
83 S_ISREG(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int result;
85
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070086 result = reiserfs_unpack(inode, filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080087 if (result) {
88 err = result;
89 goto setflags_out;
90 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070091 }
92 sd_attrs_to_i_attrs(flags, inode);
93 REISERFS_I(inode)->i_attrs = flags;
Deepa Dinamani02027d42016-09-14 07:48:05 -070094 inode->i_ctime = current_time(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070095 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -080096setflags_out:
Al Viro2a79f172011-12-09 08:06:57 -050097 mnt_drop_write_file(filp);
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 case REISERFS_IOC_GETVERSION:
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200101 err = put_user(inode->i_generation, (int __user *)arg);
102 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 case REISERFS_IOC_SETVERSION:
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700104 if (!inode_owner_or_capable(inode)) {
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200105 err = -EPERM;
106 break;
Jiri Slabye0baec12010-01-06 23:09:50 +0100107 }
Al Viroa561be72011-11-23 11:57:51 -0500108 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800109 if (err)
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200110 break;
Dave Hansen42a74f22008-02-15 14:37:46 -0800111 if (get_user(inode->i_generation, (int __user *)arg)) {
112 err = -EFAULT;
113 goto setversion_out;
114 }
Deepa Dinamani02027d42016-09-14 07:48:05 -0700115 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800117setversion_out:
Al Viro2a79f172011-12-09 08:06:57 -0500118 mnt_drop_write_file(filp);
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200119 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 default:
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200121 err = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200123
124 reiserfs_write_unlock(inode->i_sb);
125
126 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
David Howells52b499c2006-08-29 19:06:18 +0100129#ifdef CONFIG_COMPAT
130long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
131 unsigned long arg)
132{
Jeff Mahoney098297b2014-04-23 10:00:36 -0400133 /*
134 * These are just misnamed, they actually
135 * get/put from/to user an int
136 */
David Howells52b499c2006-08-29 19:06:18 +0100137 switch (cmd) {
138 case REISERFS_IOC32_UNPACK:
139 cmd = REISERFS_IOC_UNPACK;
140 break;
141 case REISERFS_IOC32_GETFLAGS:
142 cmd = REISERFS_IOC_GETFLAGS;
143 break;
144 case REISERFS_IOC32_SETFLAGS:
145 cmd = REISERFS_IOC_SETFLAGS;
146 break;
147 case REISERFS_IOC32_GETVERSION:
148 cmd = REISERFS_IOC_GETVERSION;
149 break;
150 case REISERFS_IOC32_SETVERSION:
151 cmd = REISERFS_IOC_SETVERSION;
152 break;
153 default:
154 return -ENOIOCTLCMD;
155 }
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200156
Frederic Weisbecker205cb372009-10-14 23:22:17 +0200157 return reiserfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
David Howells52b499c2006-08-29 19:06:18 +0100158}
159#endif
160
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700161int reiserfs_commit_write(struct file *f, struct page *page,
162 unsigned from, unsigned to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
Jeff Mahoney098297b2014-04-23 10:00:36 -0400164 * reiserfs_unpack
165 * Function try to convert tail from direct item into indirect.
166 * It set up nopack attribute in the REISERFS_I(inode)->nopack
167 */
Jan Karad5dee5c2008-04-28 02:16:23 -0700168int reiserfs_unpack(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700170 int retval = 0;
171 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 Weisbeckerda905872010-11-24 12:57:15 -0800186 /* we need to make sure nobody is changing the file size beneath us */
Colin Ian King5404e7e2020-04-06 20:11:46 -0700187 {
188 int depth = reiserfs_write_unlock_nested(inode->i_sb);
189
190 inode_lock(inode);
191 reiserfs_write_lock_nested(inode->i_sb, depth);
192 }
Frederic Weisbeckerda905872010-11-24 12:57:15 -0800193
Jeff Mahoney278f6672013-08-08 17:34:46 -0400194 reiserfs_write_lock(inode->i_sb);
195
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700196 write_from = inode->i_size & (blocksize - 1);
197 /* if we are on a block boundary, we are already unpacked. */
198 if (write_from == 0) {
199 REISERFS_I(inode)->i_flags |= i_nopack_mask;
200 goto out;
201 }
202
Jeff Mahoney098297b2014-04-23 10:00:36 -0400203 /*
204 * we unpack by finding the page with the tail, and calling
205 * __reiserfs_write_begin on that page. This will force a
206 * reiserfs_get_block to unpack the tail for us.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700207 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300208 index = inode->i_size >> PAGE_SHIFT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700209 mapping = inode->i_mapping;
210 page = grab_cache_page(mapping, index);
211 retval = -ENOMEM;
212 if (!page) {
213 goto out;
214 }
Christoph Hellwigebdec242010-10-06 10:47:23 +0200215 retval = __reiserfs_write_begin(page, write_from, 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700216 if (retval)
217 goto out_unlock;
218
219 /* conversion can change page contents, must flush */
220 flush_dcache_page(page);
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700221 retval = reiserfs_commit_write(NULL, page, write_from, write_from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 REISERFS_I(inode)->i_flags |= i_nopack_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400224out_unlock:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700225 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300226 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400228out:
Al Viro59551022016-01-22 15:40:57 -0500229 inode_unlock(inode);
Jeff Mahoney278f6672013-08-08 17:34:46 -0400230 reiserfs_write_unlock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700231 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}