]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README | |
3 | */ | |
4 | ||
16f7e0fe | 5 | #include <linux/capability.h> |
1da177e4 | 6 | #include <linux/fs.h> |
42a74f20 | 7 | #include <linux/mount.h> |
f466c6fd | 8 | #include "reiserfs.h" |
1da177e4 LT |
9 | #include <linux/time.h> |
10 | #include <asm/uaccess.h> | |
11 | #include <linux/pagemap.h> | |
52b499c4 | 12 | #include <linux/compat.h> |
1da177e4 | 13 | |
1da177e4 | 14 | /* |
ac78a078 FW |
15 | * reiserfs_ioctl - handler for ioctl for inode |
16 | * supported commands: | |
17 | * 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect | |
098297b2 JM |
18 | * and prevent packing file (argument arg has t |
19 | * be non-zero) | |
ac78a078 FW |
20 | * 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION |
21 | * 3) That's all for a while ... | |
22 | */ | |
205cb37b | 23 | long reiserfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
1da177e4 | 24 | { |
496ad9aa | 25 | struct inode *inode = file_inode(filp); |
1da177e4 | 26 | unsigned int flags; |
42a74f20 | 27 | int err = 0; |
1da177e4 | 28 | |
ac78a078 FW |
29 | reiserfs_write_lock(inode->i_sb); |
30 | ||
1da177e4 | 31 | switch (cmd) { |
bd4c625c LT |
32 | case REISERFS_IOC_UNPACK: |
33 | if (S_ISREG(inode->i_mode)) { | |
34 | if (arg) | |
ac78a078 | 35 | err = reiserfs_unpack(inode, filp); |
1da177e4 | 36 | } else |
ac78a078 FW |
37 | err = -ENOTTY; |
38 | break; | |
39 | /* | |
40 | * following two cases are taken from fs/ext2/ioctl.c by Remy | |
41 | * Card (card@masi.ibp.fr) | |
42 | */ | |
1da177e4 | 43 | case REISERFS_IOC_GETFLAGS: |
ac78a078 FW |
44 | if (!reiserfs_attrs(inode->i_sb)) { |
45 | err = -ENOTTY; | |
46 | break; | |
47 | } | |
869eb76e | 48 | |
bd4c625c LT |
49 | flags = REISERFS_I(inode)->i_attrs; |
50 | i_attrs_to_sd_attrs(inode, (__u16 *) & flags); | |
ac78a078 FW |
51 | err = put_user(flags, (int __user *)arg); |
52 | break; | |
bd4c625c | 53 | case REISERFS_IOC_SETFLAGS:{ |
ac78a078 FW |
54 | if (!reiserfs_attrs(inode->i_sb)) { |
55 | err = -ENOTTY; | |
56 | break; | |
57 | } | |
869eb76e | 58 | |
a561be71 | 59 | err = mnt_want_write_file(filp); |
42a74f20 | 60 | if (err) |
ac78a078 | 61 | break; |
1da177e4 | 62 | |
2e149670 | 63 | if (!inode_owner_or_capable(inode)) { |
42a74f20 DH |
64 | err = -EPERM; |
65 | goto setflags_out; | |
66 | } | |
67 | if (get_user(flags, (int __user *)arg)) { | |
68 | err = -EFAULT; | |
69 | goto setflags_out; | |
70 | } | |
71 | /* | |
72 | * Is it quota file? Do not allow user to mess with it | |
73 | */ | |
74 | if (IS_NOQUOTA(inode)) { | |
75 | err = -EPERM; | |
76 | goto setflags_out; | |
77 | } | |
bd4c625c LT |
78 | if (((flags ^ REISERFS_I(inode)-> |
79 | i_attrs) & (REISERFS_IMMUTABLE_FL | | |
80 | REISERFS_APPEND_FL)) | |
42a74f20 DH |
81 | && !capable(CAP_LINUX_IMMUTABLE)) { |
82 | err = -EPERM; | |
83 | goto setflags_out; | |
84 | } | |
bd4c625c LT |
85 | if ((flags & REISERFS_NOTAIL_FL) && |
86 | S_ISREG(inode->i_mode)) { | |
1da177e4 LT |
87 | int result; |
88 | ||
bd4c625c | 89 | result = reiserfs_unpack(inode, filp); |
42a74f20 DH |
90 | if (result) { |
91 | err = result; | |
92 | goto setflags_out; | |
93 | } | |
bd4c625c LT |
94 | } |
95 | sd_attrs_to_i_attrs(flags, inode); | |
96 | REISERFS_I(inode)->i_attrs = flags; | |
97 | inode->i_ctime = CURRENT_TIME_SEC; | |
98 | mark_inode_dirty(inode); | |
42a74f20 | 99 | setflags_out: |
2a79f17e | 100 | mnt_drop_write_file(filp); |
ac78a078 | 101 | break; |
1da177e4 | 102 | } |
1da177e4 | 103 | case REISERFS_IOC_GETVERSION: |
ac78a078 FW |
104 | err = put_user(inode->i_generation, (int __user *)arg); |
105 | break; | |
1da177e4 | 106 | case REISERFS_IOC_SETVERSION: |
2e149670 | 107 | if (!inode_owner_or_capable(inode)) { |
ac78a078 FW |
108 | err = -EPERM; |
109 | break; | |
e0baec1b | 110 | } |
a561be71 | 111 | err = mnt_want_write_file(filp); |
42a74f20 | 112 | if (err) |
ac78a078 | 113 | break; |
42a74f20 DH |
114 | if (get_user(inode->i_generation, (int __user *)arg)) { |
115 | err = -EFAULT; | |
116 | goto setversion_out; | |
117 | } | |
1da177e4 LT |
118 | inode->i_ctime = CURRENT_TIME_SEC; |
119 | mark_inode_dirty(inode); | |
42a74f20 | 120 | setversion_out: |
2a79f17e | 121 | mnt_drop_write_file(filp); |
ac78a078 | 122 | break; |
1da177e4 | 123 | default: |
ac78a078 | 124 | err = -ENOTTY; |
1da177e4 | 125 | } |
ac78a078 FW |
126 | |
127 | reiserfs_write_unlock(inode->i_sb); | |
128 | ||
129 | return err; | |
1da177e4 LT |
130 | } |
131 | ||
52b499c4 DH |
132 | #ifdef CONFIG_COMPAT |
133 | long reiserfs_compat_ioctl(struct file *file, unsigned int cmd, | |
134 | unsigned long arg) | |
135 | { | |
098297b2 JM |
136 | /* |
137 | * These are just misnamed, they actually | |
138 | * get/put from/to user an int | |
139 | */ | |
52b499c4 DH |
140 | switch (cmd) { |
141 | case REISERFS_IOC32_UNPACK: | |
142 | cmd = REISERFS_IOC_UNPACK; | |
143 | break; | |
144 | case REISERFS_IOC32_GETFLAGS: | |
145 | cmd = REISERFS_IOC_GETFLAGS; | |
146 | break; | |
147 | case REISERFS_IOC32_SETFLAGS: | |
148 | cmd = REISERFS_IOC_SETFLAGS; | |
149 | break; | |
150 | case REISERFS_IOC32_GETVERSION: | |
151 | cmd = REISERFS_IOC_GETVERSION; | |
152 | break; | |
153 | case REISERFS_IOC32_SETVERSION: | |
154 | cmd = REISERFS_IOC_SETVERSION; | |
155 | break; | |
156 | default: | |
157 | return -ENOIOCTLCMD; | |
158 | } | |
8ebc4232 | 159 | |
205cb37b | 160 | return reiserfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
52b499c4 DH |
161 | } |
162 | #endif | |
163 | ||
ba9d8cec VS |
164 | int reiserfs_commit_write(struct file *f, struct page *page, |
165 | unsigned from, unsigned to); | |
1da177e4 | 166 | /* |
098297b2 JM |
167 | * reiserfs_unpack |
168 | * Function try to convert tail from direct item into indirect. | |
169 | * It set up nopack attribute in the REISERFS_I(inode)->nopack | |
170 | */ | |
d5dee5c3 | 171 | int reiserfs_unpack(struct inode *inode, struct file *filp) |
1da177e4 | 172 | { |
bd4c625c LT |
173 | int retval = 0; |
174 | int index; | |
175 | struct page *page; | |
176 | struct address_space *mapping; | |
177 | unsigned long write_from; | |
178 | unsigned long blocksize = inode->i_sb->s_blocksize; | |
179 | ||
180 | if (inode->i_size == 0) { | |
181 | REISERFS_I(inode)->i_flags |= i_nopack_mask; | |
182 | return 0; | |
183 | } | |
184 | /* ioctl already done */ | |
185 | if (REISERFS_I(inode)->i_flags & i_nopack_mask) { | |
186 | return 0; | |
187 | } | |
bd4c625c | 188 | |
da905873 FW |
189 | /* 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 | ||
278f6679 JM |
192 | reiserfs_write_lock(inode->i_sb); |
193 | ||
bd4c625c LT |
194 | write_from = inode->i_size & (blocksize - 1); |
195 | /* if we are on a block boundary, we are already unpacked. */ | |
196 | if (write_from == 0) { | |
197 | REISERFS_I(inode)->i_flags |= i_nopack_mask; | |
198 | goto out; | |
199 | } | |
200 | ||
098297b2 JM |
201 | /* |
202 | * we unpack by finding the page with the tail, and calling | |
203 | * __reiserfs_write_begin on that page. This will force a | |
204 | * reiserfs_get_block to unpack the tail for us. | |
bd4c625c LT |
205 | */ |
206 | index = inode->i_size >> PAGE_CACHE_SHIFT; | |
207 | mapping = inode->i_mapping; | |
208 | page = grab_cache_page(mapping, index); | |
209 | retval = -ENOMEM; | |
210 | if (!page) { | |
211 | goto out; | |
212 | } | |
ebdec241 | 213 | retval = __reiserfs_write_begin(page, write_from, 0); |
bd4c625c LT |
214 | if (retval) |
215 | goto out_unlock; | |
216 | ||
217 | /* conversion can change page contents, must flush */ | |
218 | flush_dcache_page(page); | |
ba9d8cec | 219 | retval = reiserfs_commit_write(NULL, page, write_from, write_from); |
1da177e4 | 220 | REISERFS_I(inode)->i_flags |= i_nopack_mask; |
bd4c625c | 221 | |
cf776a7a | 222 | out_unlock: |
bd4c625c LT |
223 | unlock_page(page); |
224 | page_cache_release(page); | |
225 | ||
cf776a7a | 226 | out: |
1b1dcc1b | 227 | mutex_unlock(&inode->i_mutex); |
278f6679 | 228 | reiserfs_write_unlock(inode->i_sb); |
bd4c625c | 229 | return retval; |
1da177e4 | 230 | } |