blob: 745c8810089512e0b9a77d6f3847ea7683500037 [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>
12
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070013static int reiserfs_unpack(struct inode *inode, struct file *filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15/*
16** reiserfs_ioctl - handler for ioctl for inode
17** supported commands:
18** 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
19** and prevent packing file (argument arg has to be non-zero)
20** 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
21** 3) That's all for a while ...
22*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070023int reiserfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
24 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 unsigned int flags;
27
28 switch (cmd) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070029 case REISERFS_IOC_UNPACK:
30 if (S_ISREG(inode->i_mode)) {
31 if (arg)
32 return reiserfs_unpack(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 else
34 return 0;
35 } else
36 return -ENOTTY;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070037 /* following two cases are taken from fs/ext2/ioctl.c by Remy
38 Card (card@masi.ibp.fr) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 case REISERFS_IOC_GETFLAGS:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070040 if (!reiserfs_attrs(inode->i_sb))
Jeff Mahoney869eb762005-06-29 18:52:28 -040041 return -ENOTTY;
42
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070043 flags = REISERFS_I(inode)->i_attrs;
44 i_attrs_to_sd_attrs(inode, (__u16 *) & flags);
45 return put_user(flags, (int __user *)arg);
46 case REISERFS_IOC_SETFLAGS:{
47 if (!reiserfs_attrs(inode->i_sb))
48 return -ENOTTY;
Jeff Mahoney869eb762005-06-29 18:52:28 -040049
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070050 if (IS_RDONLY(inode))
51 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070053 if ((current->fsuid != inode->i_uid)
54 && !capable(CAP_FOWNER))
55 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070057 if (get_user(flags, (int __user *)arg))
58 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070060 if (((flags ^ REISERFS_I(inode)->
61 i_attrs) & (REISERFS_IMMUTABLE_FL |
62 REISERFS_APPEND_FL))
63 && !capable(CAP_LINUX_IMMUTABLE))
64 return -EPERM;
65
66 if ((flags & REISERFS_NOTAIL_FL) &&
67 S_ISREG(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 int result;
69
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070070 result = reiserfs_unpack(inode, filp);
71 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return result;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070073 }
74 sd_attrs_to_i_attrs(flags, inode);
75 REISERFS_I(inode)->i_attrs = flags;
76 inode->i_ctime = CURRENT_TIME_SEC;
77 mark_inode_dirty(inode);
78 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 case REISERFS_IOC_GETVERSION:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070081 return put_user(inode->i_generation, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 case REISERFS_IOC_SETVERSION:
83 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
84 return -EPERM;
85 if (IS_RDONLY(inode))
86 return -EROFS;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070087 if (get_user(inode->i_generation, (int __user *)arg))
88 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 inode->i_ctime = CURRENT_TIME_SEC;
90 mark_inode_dirty(inode);
91 return 0;
92 default:
93 return -ENOTTY;
94 }
95}
96
97/*
98** reiserfs_unpack
99** Function try to convert tail from direct item into indirect.
100** It set up nopack attribute in the REISERFS_I(inode)->nopack
101*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700102static int reiserfs_unpack(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700104 int retval = 0;
105 int index;
106 struct page *page;
107 struct address_space *mapping;
108 unsigned long write_from;
109 unsigned long blocksize = inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700111 if (inode->i_size == 0) {
112 REISERFS_I(inode)->i_flags |= i_nopack_mask;
113 return 0;
114 }
115 /* ioctl already done */
116 if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
117 return 0;
118 }
119 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700121 /* we need to make sure nobody is changing the file size beneath
122 ** us
123 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800124 mutex_lock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700125
126 write_from = inode->i_size & (blocksize - 1);
127 /* if we are on a block boundary, we are already unpacked. */
128 if (write_from == 0) {
129 REISERFS_I(inode)->i_flags |= i_nopack_mask;
130 goto out;
131 }
132
133 /* we unpack by finding the page with the tail, and calling
134 ** reiserfs_prepare_write on that page. This will force a
135 ** reiserfs_get_block to unpack the tail for us.
136 */
137 index = inode->i_size >> PAGE_CACHE_SHIFT;
138 mapping = inode->i_mapping;
139 page = grab_cache_page(mapping, index);
140 retval = -ENOMEM;
141 if (!page) {
142 goto out;
143 }
144 retval =
145 mapping->a_ops->prepare_write(NULL, page, write_from, write_from);
146 if (retval)
147 goto out_unlock;
148
149 /* conversion can change page contents, must flush */
150 flush_dcache_page(page);
151 retval =
152 mapping->a_ops->commit_write(NULL, page, write_from, write_from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 REISERFS_I(inode)->i_flags |= i_nopack_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700155 out_unlock:
156 unlock_page(page);
157 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700159 out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800160 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700161 reiserfs_write_unlock(inode->i_sb);
162 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}