blob: 4b86ecf5817e3a227cee7cf82529211587df75db [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>
Miklos Szeredi03eb6062021-04-07 14:36:44 +020013#include <linux/fileattr.h>
14
15int reiserfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
16{
17 struct inode *inode = d_inode(dentry);
18
19 if (!reiserfs_attrs(inode->i_sb))
20 return -ENOTTY;
21
22 fileattr_fill_flags(fa, REISERFS_I(inode)->i_attrs);
23
24 return 0;
25}
26
27int reiserfs_fileattr_set(struct user_namespace *mnt_userns,
28 struct dentry *dentry, struct fileattr *fa)
29{
30 struct inode *inode = d_inode(dentry);
31 unsigned int flags = fa->flags;
32 int err;
33
34 reiserfs_write_lock(inode->i_sb);
35
36 err = -ENOTTY;
37 if (!reiserfs_attrs(inode->i_sb))
38 goto unlock;
39
40 err = -EOPNOTSUPP;
41 if (fileattr_has_fsx(fa))
42 goto unlock;
43
44 /*
45 * Is it quota file? Do not allow user to mess with it
46 */
47 err = -EPERM;
48 if (IS_NOQUOTA(inode))
49 goto unlock;
50
51 if ((flags & REISERFS_NOTAIL_FL) && S_ISREG(inode->i_mode)) {
52 err = reiserfs_unpack(inode);
53 if (err)
54 goto unlock;
55 }
56 sd_attrs_to_i_attrs(flags, inode);
57 REISERFS_I(inode)->i_attrs = flags;
58 inode->i_ctime = current_time(inode);
59 mark_inode_dirty(inode);
60 err = 0;
61unlock:
62 reiserfs_write_unlock(inode->i_sb);
63
64 return err;
65}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020068 * reiserfs_ioctl - handler for ioctl for inode
69 * supported commands:
70 * 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
Jeff Mahoney098297b2014-04-23 10:00:36 -040071 * and prevent packing file (argument arg has t
72 * be non-zero)
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020073 * 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
74 * 3) That's all for a while ...
75 */
Frederic Weisbecker205cb372009-10-14 23:22:17 +020076long reiserfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Al Viro496ad9a2013-01-23 17:07:38 -050078 struct inode *inode = file_inode(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -080079 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020081 reiserfs_write_lock(inode->i_sb);
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 switch (cmd) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070084 case REISERFS_IOC_UNPACK:
85 if (S_ISREG(inode->i_mode)) {
86 if (arg)
Miklos Szeredi03eb6062021-04-07 14:36:44 +020087 err = reiserfs_unpack(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 } else
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020089 err = -ENOTTY;
90 break;
91 /*
92 * following two cases are taken from fs/ext2/ioctl.c by Remy
93 * Card (card@masi.ibp.fr)
94 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 case REISERFS_IOC_GETVERSION:
Frederic Weisbeckerac78a072009-10-14 23:08:43 +020096 err = put_user(inode->i_generation, (int __user *)arg);
97 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 case REISERFS_IOC_SETVERSION:
Christian Brauner21cb47b2021-01-21 14:19:25 +010099 if (!inode_owner_or_capable(&init_user_ns, inode)) {
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200100 err = -EPERM;
101 break;
Jiri Slabye0baec12010-01-06 23:09:50 +0100102 }
Al Viroa561be72011-11-23 11:57:51 -0500103 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800104 if (err)
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200105 break;
Dave Hansen42a74f22008-02-15 14:37:46 -0800106 if (get_user(inode->i_generation, (int __user *)arg)) {
107 err = -EFAULT;
108 goto setversion_out;
109 }
Deepa Dinamani02027d42016-09-14 07:48:05 -0700110 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 mark_inode_dirty(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800112setversion_out:
Al Viro2a79f172011-12-09 08:06:57 -0500113 mnt_drop_write_file(filp);
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200114 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 default:
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200116 err = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Frederic Weisbeckerac78a072009-10-14 23:08:43 +0200118
119 reiserfs_write_unlock(inode->i_sb);
120
121 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
David Howells52b499c2006-08-29 19:06:18 +0100124#ifdef CONFIG_COMPAT
125long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
126 unsigned long arg)
127{
Jeff Mahoney098297b2014-04-23 10:00:36 -0400128 /*
129 * These are just misnamed, they actually
130 * get/put from/to user an int
131 */
David Howells52b499c2006-08-29 19:06:18 +0100132 switch (cmd) {
133 case REISERFS_IOC32_UNPACK:
134 cmd = REISERFS_IOC_UNPACK;
135 break;
David Howells52b499c2006-08-29 19:06:18 +0100136 case REISERFS_IOC32_GETVERSION:
137 cmd = REISERFS_IOC_GETVERSION;
138 break;
139 case REISERFS_IOC32_SETVERSION:
140 cmd = REISERFS_IOC_SETVERSION;
141 break;
142 default:
143 return -ENOIOCTLCMD;
144 }
Frederic Weisbecker8ebc4232009-04-07 04:19:49 +0200145
Frederic Weisbecker205cb372009-10-14 23:22:17 +0200146 return reiserfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
David Howells52b499c2006-08-29 19:06:18 +0100147}
148#endif
149
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700150int reiserfs_commit_write(struct file *f, struct page *page,
151 unsigned from, unsigned to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
Jeff Mahoney098297b2014-04-23 10:00:36 -0400153 * reiserfs_unpack
154 * Function try to convert tail from direct item into indirect.
155 * It set up nopack attribute in the REISERFS_I(inode)->nopack
156 */
Miklos Szeredi03eb6062021-04-07 14:36:44 +0200157int reiserfs_unpack(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700159 int retval = 0;
160 int index;
161 struct page *page;
162 struct address_space *mapping;
163 unsigned long write_from;
164 unsigned long blocksize = inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700166 if (inode->i_size == 0) {
167 REISERFS_I(inode)->i_flags |= i_nopack_mask;
168 return 0;
169 }
170 /* ioctl already done */
171 if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
172 return 0;
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Frederic Weisbeckerda905872010-11-24 12:57:15 -0800175 /* we need to make sure nobody is changing the file size beneath us */
Colin Ian King5404e7e2020-04-06 20:11:46 -0700176 {
177 int depth = reiserfs_write_unlock_nested(inode->i_sb);
178
179 inode_lock(inode);
180 reiserfs_write_lock_nested(inode->i_sb, depth);
181 }
Frederic Weisbeckerda905872010-11-24 12:57:15 -0800182
Jeff Mahoney278f6672013-08-08 17:34:46 -0400183 reiserfs_write_lock(inode->i_sb);
184
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700185 write_from = inode->i_size & (blocksize - 1);
186 /* if we are on a block boundary, we are already unpacked. */
187 if (write_from == 0) {
188 REISERFS_I(inode)->i_flags |= i_nopack_mask;
189 goto out;
190 }
191
Jeff Mahoney098297b2014-04-23 10:00:36 -0400192 /*
193 * we unpack by finding the page with the tail, and calling
194 * __reiserfs_write_begin on that page. This will force a
195 * reiserfs_get_block to unpack the tail for us.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700196 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300197 index = inode->i_size >> PAGE_SHIFT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700198 mapping = inode->i_mapping;
199 page = grab_cache_page(mapping, index);
200 retval = -ENOMEM;
201 if (!page) {
202 goto out;
203 }
Christoph Hellwigebdec242010-10-06 10:47:23 +0200204 retval = __reiserfs_write_begin(page, write_from, 0);
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
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400213out_unlock:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700214 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300215 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400217out:
Al Viro59551022016-01-22 15:40:57 -0500218 inode_unlock(inode);
Jeff Mahoney278f6672013-08-08 17:34:46 -0400219 reiserfs_write_unlock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700220 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}