blob: b484d2913c0dc18389fe242fd9c1568d578dd82f [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>
7#include <linux/reiserfs_fs.h>
8#include <linux/time.h>
9#include <asm/uaccess.h>
10#include <linux/pagemap.h>
11#include <linux/smp_lock.h>
David Howells52b499c2006-08-29 19:06:18 +010012#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070014static int reiserfs_unpack(struct inode *inode, struct file *filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/*
17** reiserfs_ioctl - handler for ioctl for inode
18** supported commands:
19** 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
20** and prevent packing file (argument arg has to be non-zero)
21** 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
22** 3) That's all for a while ...
23*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070024int reiserfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
25 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
27 unsigned int flags;
28
29 switch (cmd) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070030 case REISERFS_IOC_UNPACK:
31 if (S_ISREG(inode->i_mode)) {
32 if (arg)
33 return reiserfs_unpack(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 else
35 return 0;
36 } else
37 return -ENOTTY;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070038 /* following two cases are taken from fs/ext2/ioctl.c by Remy
39 Card (card@masi.ibp.fr) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 case REISERFS_IOC_GETFLAGS:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070041 if (!reiserfs_attrs(inode->i_sb))
Jeff Mahoney869eb762005-06-29 18:52:28 -040042 return -ENOTTY;
43
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070044 flags = REISERFS_I(inode)->i_attrs;
45 i_attrs_to_sd_attrs(inode, (__u16 *) & flags);
46 return put_user(flags, (int __user *)arg);
47 case REISERFS_IOC_SETFLAGS:{
48 if (!reiserfs_attrs(inode->i_sb))
49 return -ENOTTY;
Jeff Mahoney869eb762005-06-29 18:52:28 -040050
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070051 if (IS_RDONLY(inode))
52 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070054 if ((current->fsuid != inode->i_uid)
55 && !capable(CAP_FOWNER))
56 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070058 if (get_user(flags, (int __user *)arg))
59 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070061 if (((flags ^ REISERFS_I(inode)->
62 i_attrs) & (REISERFS_IMMUTABLE_FL |
63 REISERFS_APPEND_FL))
64 && !capable(CAP_LINUX_IMMUTABLE))
65 return -EPERM;
66
67 if ((flags & REISERFS_NOTAIL_FL) &&
68 S_ISREG(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 int result;
70
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070071 result = reiserfs_unpack(inode, filp);
72 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return result;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070074 }
75 sd_attrs_to_i_attrs(flags, inode);
76 REISERFS_I(inode)->i_attrs = flags;
77 inode->i_ctime = CURRENT_TIME_SEC;
78 mark_inode_dirty(inode);
79 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 case REISERFS_IOC_GETVERSION:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070082 return put_user(inode->i_generation, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 case REISERFS_IOC_SETVERSION:
84 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
85 return -EPERM;
86 if (IS_RDONLY(inode))
87 return -EROFS;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070088 if (get_user(inode->i_generation, (int __user *)arg))
89 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 inode->i_ctime = CURRENT_TIME_SEC;
91 mark_inode_dirty(inode);
92 return 0;
93 default:
94 return -ENOTTY;
95 }
96}
97
David Howells52b499c2006-08-29 19:06:18 +010098#ifdef CONFIG_COMPAT
99long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
100 unsigned long arg)
101{
Josef Sipek1fc5adb2006-12-08 02:37:33 -0800102 struct inode *inode = file->f_path.dentry->d_inode;
David Howells52b499c2006-08-29 19:06:18 +0100103 int ret;
104
105 /* These are just misnamed, they actually get/put from/to user an int */
106 switch (cmd) {
107 case REISERFS_IOC32_UNPACK:
108 cmd = REISERFS_IOC_UNPACK;
109 break;
110 case REISERFS_IOC32_GETFLAGS:
111 cmd = REISERFS_IOC_GETFLAGS;
112 break;
113 case REISERFS_IOC32_SETFLAGS:
114 cmd = REISERFS_IOC_SETFLAGS;
115 break;
116 case REISERFS_IOC32_GETVERSION:
117 cmd = REISERFS_IOC_GETVERSION;
118 break;
119 case REISERFS_IOC32_SETVERSION:
120 cmd = REISERFS_IOC_SETVERSION;
121 break;
122 default:
123 return -ENOIOCTLCMD;
124 }
125 lock_kernel();
126 ret = reiserfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
127 unlock_kernel();
128 return ret;
129}
130#endif
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
133** reiserfs_unpack
134** Function try to convert tail from direct item into indirect.
135** It set up nopack attribute in the REISERFS_I(inode)->nopack
136*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700137static int reiserfs_unpack(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700139 int retval = 0;
140 int index;
141 struct page *page;
142 struct address_space *mapping;
143 unsigned long write_from;
144 unsigned long blocksize = inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700146 if (inode->i_size == 0) {
147 REISERFS_I(inode)->i_flags |= i_nopack_mask;
148 return 0;
149 }
150 /* ioctl already done */
151 if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
152 return 0;
153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700155 /* we need to make sure nobody is changing the file size beneath
156 ** us
157 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800158 mutex_lock(&inode->i_mutex);
Chris Masonb5f39532006-08-05 12:15:08 -0700159 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700160
161 write_from = inode->i_size & (blocksize - 1);
162 /* if we are on a block boundary, we are already unpacked. */
163 if (write_from == 0) {
164 REISERFS_I(inode)->i_flags |= i_nopack_mask;
165 goto out;
166 }
167
168 /* we unpack by finding the page with the tail, and calling
169 ** reiserfs_prepare_write on that page. This will force a
170 ** reiserfs_get_block to unpack the tail for us.
171 */
172 index = inode->i_size >> PAGE_CACHE_SHIFT;
173 mapping = inode->i_mapping;
174 page = grab_cache_page(mapping, index);
175 retval = -ENOMEM;
176 if (!page) {
177 goto out;
178 }
179 retval =
180 mapping->a_ops->prepare_write(NULL, page, write_from, write_from);
181 if (retval)
182 goto out_unlock;
183
184 /* conversion can change page contents, must flush */
185 flush_dcache_page(page);
186 retval =
187 mapping->a_ops->commit_write(NULL, page, write_from, write_from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 REISERFS_I(inode)->i_flags |= i_nopack_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700190 out_unlock:
191 unlock_page(page);
192 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194 out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800195 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700196 reiserfs_write_unlock(inode->i_sb);
197 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}