ioctl.c 4.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
 */

#include <linux/fs.h>
#include <linux/reiserfs_fs.h>
#include <linux/time.h>
#include <asm/uaccess.h>
#include <linux/pagemap.h>
#include <linux/smp_lock.h>

12
static int reiserfs_unpack(struct inode *inode, struct file *filp);
L
Linus Torvalds 已提交
13 14 15 16 17 18 19 20 21

/*
** 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 ...
*/
22 23
int reiserfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
		   unsigned long arg)
L
Linus Torvalds 已提交
24 25 26 27
{
	unsigned int flags;

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

42 43 44 45 46 47
		flags = REISERFS_I(inode)->i_attrs;
		i_attrs_to_sd_attrs(inode, (__u16 *) & flags);
		return put_user(flags, (int __user *)arg);
	case REISERFS_IOC_SETFLAGS:{
			if (!reiserfs_attrs(inode->i_sb))
				return -ENOTTY;
48

49 50
			if (IS_RDONLY(inode))
				return -EROFS;
L
Linus Torvalds 已提交
51

52 53 54
			if ((current->fsuid != inode->i_uid)
			    && !capable(CAP_FOWNER))
				return -EPERM;
L
Linus Torvalds 已提交
55

56 57
			if (get_user(flags, (int __user *)arg))
				return -EFAULT;
L
Linus Torvalds 已提交
58

59 60 61 62 63 64 65 66
			if (((flags ^ REISERFS_I(inode)->
			      i_attrs) & (REISERFS_IMMUTABLE_FL |
					  REISERFS_APPEND_FL))
			    && !capable(CAP_LINUX_IMMUTABLE))
				return -EPERM;

			if ((flags & REISERFS_NOTAIL_FL) &&
			    S_ISREG(inode->i_mode)) {
L
Linus Torvalds 已提交
67 68
				int result;

69 70
				result = reiserfs_unpack(inode, filp);
				if (result)
L
Linus Torvalds 已提交
71
					return result;
72 73 74 75 76 77
			}
			sd_attrs_to_i_attrs(flags, inode);
			REISERFS_I(inode)->i_attrs = flags;
			inode->i_ctime = CURRENT_TIME_SEC;
			mark_inode_dirty(inode);
			return 0;
L
Linus Torvalds 已提交
78 79
		}
	case REISERFS_IOC_GETVERSION:
80
		return put_user(inode->i_generation, (int __user *)arg);
L
Linus Torvalds 已提交
81 82 83 84 85
	case REISERFS_IOC_SETVERSION:
		if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
			return -EPERM;
		if (IS_RDONLY(inode))
			return -EROFS;
86 87
		if (get_user(inode->i_generation, (int __user *)arg))
			return -EFAULT;
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100
		inode->i_ctime = CURRENT_TIME_SEC;
		mark_inode_dirty(inode);
		return 0;
	default:
		return -ENOTTY;
	}
}

/*
** reiserfs_unpack
** Function try to convert tail from direct item into indirect.
** It set up nopack attribute in the REISERFS_I(inode)->nopack
*/
101
static int reiserfs_unpack(struct inode *inode, struct file *filp)
L
Linus Torvalds 已提交
102
{
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	int retval = 0;
	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;
	}
	reiserfs_write_lock(inode->i_sb);

	/* we need to make sure nobody is changing the file size beneath
	 ** us
	 */
	down(&inode->i_sem);

	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
	 ** reiserfs_prepare_write on that page.  This will force a 
	 ** 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;
	}
	retval =
	    mapping->a_ops->prepare_write(NULL, page, write_from, write_from);
	if (retval)
		goto out_unlock;

	/* conversion can change page contents, must flush */
	flush_dcache_page(page);
	retval =
	    mapping->a_ops->commit_write(NULL, page, write_from, write_from);
L
Linus Torvalds 已提交
152
	REISERFS_I(inode)->i_flags |= i_nopack_mask;
153 154 155 156 157 158 159 160 161

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

      out:
	up(&inode->i_sem);
	reiserfs_write_unlock(inode->i_sb);
	return retval;
L
Linus Torvalds 已提交
162
}