ioctl.c 5.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
 */

5
#include <linux/capability.h>
L
Linus Torvalds 已提交
6
#include <linux/fs.h>
7
#include <linux/mount.h>
L
Linus Torvalds 已提交
8 9 10 11 12
#include <linux/reiserfs_fs.h>
#include <linux/time.h>
#include <asm/uaccess.h>
#include <linux/pagemap.h>
#include <linux/smp_lock.h>
13
#include <linux/compat.h>
L
Linus Torvalds 已提交
14 15

/*
16 17 18 19 20 21 22
 * reiserfs_ioctl - handler for ioctl for inode
 * supported commands:
 *  1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
 *                           and prevent packing file (argument arg has to be non-zero)
 *  2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
 *  3) That's all for a while ...
 */
23
long reiserfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
24
{
25
	struct inode *inode = filp->f_path.dentry->d_inode;
L
Linus Torvalds 已提交
26
	unsigned int flags;
27
	int err = 0;
L
Linus Torvalds 已提交
28

29 30
	reiserfs_write_lock(inode->i_sb);

L
Linus Torvalds 已提交
31
	switch (cmd) {
32 33 34
	case REISERFS_IOC_UNPACK:
		if (S_ISREG(inode->i_mode)) {
			if (arg)
35
				err = reiserfs_unpack(inode, filp);
L
Linus Torvalds 已提交
36
		} else
37 38 39 40 41 42
			err = -ENOTTY;
		break;
		/*
		 * following two cases are taken from fs/ext2/ioctl.c by Remy
		 * Card (card@masi.ibp.fr)
		 */
L
Linus Torvalds 已提交
43
	case REISERFS_IOC_GETFLAGS:
44 45 46 47
		if (!reiserfs_attrs(inode->i_sb)) {
			err = -ENOTTY;
			break;
		}
48

49 50
		flags = REISERFS_I(inode)->i_attrs;
		i_attrs_to_sd_attrs(inode, (__u16 *) & flags);
51 52
		err = put_user(flags, (int __user *)arg);
		break;
53
	case REISERFS_IOC_SETFLAGS:{
54 55 56 57
			if (!reiserfs_attrs(inode->i_sb)) {
				err = -ENOTTY;
				break;
			}
58

59 60
			err = mnt_want_write(filp->f_path.mnt);
			if (err)
61
				break;
L
Linus Torvalds 已提交
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
			if (!is_owner_or_cap(inode)) {
				err = -EPERM;
				goto setflags_out;
			}
			if (get_user(flags, (int __user *)arg)) {
				err = -EFAULT;
				goto setflags_out;
			}
			/*
			 * Is it quota file? Do not allow user to mess with it
			 */
			if (IS_NOQUOTA(inode)) {
				err = -EPERM;
				goto setflags_out;
			}
78 79 80
			if (((flags ^ REISERFS_I(inode)->
			      i_attrs) & (REISERFS_IMMUTABLE_FL |
					  REISERFS_APPEND_FL))
81 82 83 84
			    && !capable(CAP_LINUX_IMMUTABLE)) {
				err = -EPERM;
				goto setflags_out;
			}
85 86
			if ((flags & REISERFS_NOTAIL_FL) &&
			    S_ISREG(inode->i_mode)) {
L
Linus Torvalds 已提交
87 88
				int result;

89
				result = reiserfs_unpack(inode, filp);
90 91 92 93
				if (result) {
					err = result;
					goto setflags_out;
				}
94 95 96 97 98
			}
			sd_attrs_to_i_attrs(flags, inode);
			REISERFS_I(inode)->i_attrs = flags;
			inode->i_ctime = CURRENT_TIME_SEC;
			mark_inode_dirty(inode);
99 100
setflags_out:
			mnt_drop_write(filp->f_path.mnt);
101
			break;
L
Linus Torvalds 已提交
102 103
		}
	case REISERFS_IOC_GETVERSION:
104 105
		err = put_user(inode->i_generation, (int __user *)arg);
		break;
L
Linus Torvalds 已提交
106
	case REISERFS_IOC_SETVERSION:
J
Jiri Slaby 已提交
107
		if (!is_owner_or_cap(inode)) {
108 109
			err = -EPERM;
			break;
J
Jiri Slaby 已提交
110
		}
111 112
		err = mnt_want_write(filp->f_path.mnt);
		if (err)
113
			break;
114 115 116 117
		if (get_user(inode->i_generation, (int __user *)arg)) {
			err = -EFAULT;
			goto setversion_out;
		}
L
Linus Torvalds 已提交
118 119
		inode->i_ctime = CURRENT_TIME_SEC;
		mark_inode_dirty(inode);
120 121
setversion_out:
		mnt_drop_write(filp->f_path.mnt);
122
		break;
L
Linus Torvalds 已提交
123
	default:
124
		err = -ENOTTY;
L
Linus Torvalds 已提交
125
	}
126 127 128 129

	reiserfs_write_unlock(inode->i_sb);

	return err;
L
Linus Torvalds 已提交
130 131
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#ifdef CONFIG_COMPAT
long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
				unsigned long arg)
{
	/* These are just misnamed, they actually get/put from/to user an int */
	switch (cmd) {
	case REISERFS_IOC32_UNPACK:
		cmd = REISERFS_IOC_UNPACK;
		break;
	case REISERFS_IOC32_GETFLAGS:
		cmd = REISERFS_IOC_GETFLAGS;
		break;
	case REISERFS_IOC32_SETFLAGS:
		cmd = REISERFS_IOC_SETFLAGS;
		break;
	case REISERFS_IOC32_GETVERSION:
		cmd = REISERFS_IOC_GETVERSION;
		break;
	case REISERFS_IOC32_SETVERSION:
		cmd = REISERFS_IOC_SETVERSION;
		break;
	default:
		return -ENOIOCTLCMD;
	}
F
Frederic Weisbecker 已提交
156

157
	return reiserfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
158 159 160
}
#endif

V
Vladimir Saveliev 已提交
161 162
int reiserfs_commit_write(struct file *f, struct page *page,
			  unsigned from, unsigned to);
L
Linus Torvalds 已提交
163 164 165 166 167
/*
** reiserfs_unpack
** Function try to convert tail from direct item into indirect.
** It set up nopack attribute in the REISERFS_I(inode)->nopack
*/
J
Jan Kara 已提交
168
int reiserfs_unpack(struct inode *inode, struct file *filp)
L
Linus Torvalds 已提交
169
{
170
	int retval = 0;
171
	int depth;
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	int index;
	struct page *page;
	struct address_space *mapping;
	unsigned long write_from;
	unsigned long blocksize = inode->i_sb->s_blocksize;

	if (inode->i_size == 0) {
		REISERFS_I(inode)->i_flags |= i_nopack_mask;
		return 0;
	}
	/* ioctl already done */
	if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
		return 0;
	}

	/* we need to make sure nobody is changing the file size beneath
	 ** us
	 */
190
	reiserfs_mutex_lock_safe(&inode->i_mutex, inode->i_sb);
191
	depth = reiserfs_write_lock_once(inode->i_sb);
192 193 194 195 196 197 198 199 200

	write_from = inode->i_size & (blocksize - 1);
	/* if we are on a block boundary, we are already unpacked.  */
	if (write_from == 0) {
		REISERFS_I(inode)->i_flags |= i_nopack_mask;
		goto out;
	}

	/* we unpack by finding the page with the tail, and calling
C
Christoph Hellwig 已提交
201
	 ** __reiserfs_write_begin on that page.  This will force a
202 203 204 205 206 207 208 209 210
	 ** reiserfs_get_block to unpack the tail for us.
	 */
	index = inode->i_size >> PAGE_CACHE_SHIFT;
	mapping = inode->i_mapping;
	page = grab_cache_page(mapping, index);
	retval = -ENOMEM;
	if (!page) {
		goto out;
	}
C
Christoph Hellwig 已提交
211
	retval = __reiserfs_write_begin(page, write_from, 0);
212 213 214 215 216
	if (retval)
		goto out_unlock;

	/* conversion can change page contents, must flush */
	flush_dcache_page(page);
V
Vladimir Saveliev 已提交
217
	retval = reiserfs_commit_write(NULL, page, write_from, write_from);
L
Linus Torvalds 已提交
218
	REISERFS_I(inode)->i_flags |= i_nopack_mask;
219 220 221 222 223 224

      out_unlock:
	unlock_page(page);
	page_cache_release(page);

      out:
225
	mutex_unlock(&inode->i_mutex);
226
	reiserfs_write_unlock_once(inode->i_sb, depth);
227
	return retval;
L
Linus Torvalds 已提交
228
}