2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
5 #include <linux/capability.h>
7 #include <linux/mount.h>
9 #include <linux/time.h>
10 #include <linux/uaccess.h>
11 #include <linux/pagemap.h>
12 #include <linux/compat.h>
15 * reiserfs_ioctl - handler for ioctl for inode
17 * 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
18 * and prevent packing file (argument arg has t
20 * 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
21 * 3) That's all for a while ...
23 long reiserfs_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
25 struct inode
*inode
= file_inode(filp
);
29 reiserfs_write_lock(inode
->i_sb
);
32 case REISERFS_IOC_UNPACK
:
33 if (S_ISREG(inode
->i_mode
)) {
35 err
= reiserfs_unpack(inode
, filp
);
40 * following two cases are taken from fs/ext2/ioctl.c by Remy
41 * Card (card@masi.ibp.fr)
43 case REISERFS_IOC_GETFLAGS
:
44 if (!reiserfs_attrs(inode
->i_sb
)) {
49 flags
= REISERFS_I(inode
)->i_attrs
;
50 i_attrs_to_sd_attrs(inode
, (__u16
*) & flags
);
51 err
= put_user(flags
, (int __user
*)arg
);
53 case REISERFS_IOC_SETFLAGS
:{
54 if (!reiserfs_attrs(inode
->i_sb
)) {
59 err
= mnt_want_write_file(filp
);
63 if (!inode_owner_or_capable(inode
)) {
67 if (get_user(flags
, (int __user
*)arg
)) {
72 * Is it quota file? Do not allow user to mess with it
74 if (IS_NOQUOTA(inode
)) {
78 if (((flags
^ REISERFS_I(inode
)->
79 i_attrs
) & (REISERFS_IMMUTABLE_FL
|
81 && !capable(CAP_LINUX_IMMUTABLE
)) {
85 if ((flags
& REISERFS_NOTAIL_FL
) &&
86 S_ISREG(inode
->i_mode
)) {
89 result
= reiserfs_unpack(inode
, filp
);
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
);
100 mnt_drop_write_file(filp
);
103 case REISERFS_IOC_GETVERSION
:
104 err
= put_user(inode
->i_generation
, (int __user
*)arg
);
106 case REISERFS_IOC_SETVERSION
:
107 if (!inode_owner_or_capable(inode
)) {
111 err
= mnt_want_write_file(filp
);
114 if (get_user(inode
->i_generation
, (int __user
*)arg
)) {
118 inode
->i_ctime
= CURRENT_TIME_SEC
;
119 mark_inode_dirty(inode
);
121 mnt_drop_write_file(filp
);
127 reiserfs_write_unlock(inode
->i_sb
);
133 long reiserfs_compat_ioctl(struct file
*file
, unsigned int cmd
,
137 * These are just misnamed, they actually
138 * get/put from/to user an int
141 case REISERFS_IOC32_UNPACK
:
142 cmd
= REISERFS_IOC_UNPACK
;
144 case REISERFS_IOC32_GETFLAGS
:
145 cmd
= REISERFS_IOC_GETFLAGS
;
147 case REISERFS_IOC32_SETFLAGS
:
148 cmd
= REISERFS_IOC_SETFLAGS
;
150 case REISERFS_IOC32_GETVERSION
:
151 cmd
= REISERFS_IOC_GETVERSION
;
153 case REISERFS_IOC32_SETVERSION
:
154 cmd
= REISERFS_IOC_SETVERSION
;
160 return reiserfs_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));
164 int reiserfs_commit_write(struct file
*f
, struct page
*page
,
165 unsigned from
, unsigned to
);
168 * Function try to convert tail from direct item into indirect.
169 * It set up nopack attribute in the REISERFS_I(inode)->nopack
171 int reiserfs_unpack(struct inode
*inode
, struct file
*filp
)
176 struct address_space
*mapping
;
177 unsigned long write_from
;
178 unsigned long blocksize
= inode
->i_sb
->s_blocksize
;
180 if (inode
->i_size
== 0) {
181 REISERFS_I(inode
)->i_flags
|= i_nopack_mask
;
184 /* ioctl already done */
185 if (REISERFS_I(inode
)->i_flags
& i_nopack_mask
) {
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
);
192 reiserfs_write_lock(inode
->i_sb
);
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
;
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.
206 index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
207 mapping
= inode
->i_mapping
;
208 page
= grab_cache_page(mapping
, index
);
213 retval
= __reiserfs_write_begin(page
, write_from
, 0);
217 /* conversion can change page contents, must flush */
218 flush_dcache_page(page
);
219 retval
= reiserfs_commit_write(NULL
, page
, write_from
, write_from
);
220 REISERFS_I(inode
)->i_flags
|= i_nopack_mask
;
224 page_cache_release(page
);
227 mutex_unlock(&inode
->i_mutex
);
228 reiserfs_write_unlock(inode
->i_sb
);