2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
5 #include <linux/capability.h>
7 #include <linux/mount.h>
8 #include <linux/reiserfs_fs.h>
9 #include <linux/time.h>
10 #include <asm/uaccess.h>
11 #include <linux/pagemap.h>
12 #include <linux/smp_lock.h>
13 #include <linux/compat.h>
15 static int reiserfs_unpack(struct inode *inode, struct file *filp);
18 ** reiserfs_ioctl - handler for ioctl for inode
19 ** supported commands:
20 ** 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
21 ** and prevent packing file (argument arg has to be non-zero)
22 ** 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
23 ** 3) That's all for a while ...
25 int reiserfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
32 case REISERFS_IOC_UNPACK:
33 if (S_ISREG(inode->i_mode)) {
35 return reiserfs_unpack(inode, filp);
40 /* following two cases are taken from fs/ext2/ioctl.c by Remy
41 Card (card@masi.ibp.fr) */
42 case REISERFS_IOC_GETFLAGS:
43 if (!reiserfs_attrs(inode->i_sb))
46 flags = REISERFS_I(inode)->i_attrs;
47 i_attrs_to_sd_attrs(inode, (__u16 *) & flags);
48 return put_user(flags, (int __user *)arg);
49 case REISERFS_IOC_SETFLAGS:{
50 if (!reiserfs_attrs(inode->i_sb))
53 err = mnt_want_write(filp->f_path.mnt);
57 if (!is_owner_or_cap(inode)) {
61 if (get_user(flags, (int __user *)arg)) {
66 * Is it quota file? Do not allow user to mess with it
68 if (IS_NOQUOTA(inode)) {
72 if (((flags ^ REISERFS_I(inode)->
73 i_attrs) & (REISERFS_IMMUTABLE_FL |
75 && !capable(CAP_LINUX_IMMUTABLE)) {
79 if ((flags & REISERFS_NOTAIL_FL) &&
80 S_ISREG(inode->i_mode)) {
83 result = reiserfs_unpack(inode, filp);
89 sd_attrs_to_i_attrs(flags, inode);
90 REISERFS_I(inode)->i_attrs = flags;
91 inode->i_ctime = CURRENT_TIME_SEC;
92 mark_inode_dirty(inode);
94 mnt_drop_write(filp->f_path.mnt);
97 case REISERFS_IOC_GETVERSION:
98 return put_user(inode->i_generation, (int __user *)arg);
99 case REISERFS_IOC_SETVERSION:
100 if (!is_owner_or_cap(inode))
102 err = mnt_want_write(filp->f_path.mnt);
105 if (get_user(inode->i_generation, (int __user *)arg)) {
109 inode->i_ctime = CURRENT_TIME_SEC;
110 mark_inode_dirty(inode);
112 mnt_drop_write(filp->f_path.mnt);
120 long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
123 struct inode *inode = file->f_path.dentry->d_inode;
126 /* These are just misnamed, they actually get/put from/to user an int */
128 case REISERFS_IOC32_UNPACK:
129 cmd = REISERFS_IOC_UNPACK;
131 case REISERFS_IOC32_GETFLAGS:
132 cmd = REISERFS_IOC_GETFLAGS;
134 case REISERFS_IOC32_SETFLAGS:
135 cmd = REISERFS_IOC_SETFLAGS;
137 case REISERFS_IOC32_GETVERSION:
138 cmd = REISERFS_IOC_GETVERSION;
140 case REISERFS_IOC32_SETVERSION:
141 cmd = REISERFS_IOC_SETVERSION;
147 ret = reiserfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
153 int reiserfs_commit_write(struct file *f, struct page *page,
154 unsigned from, unsigned to);
155 int reiserfs_prepare_write(struct file *f, struct page *page,
156 unsigned from, unsigned to);
159 ** Function try to convert tail from direct item into indirect.
160 ** It set up nopack attribute in the REISERFS_I(inode)->nopack
162 static int reiserfs_unpack(struct inode *inode, struct file *filp)
167 struct address_space *mapping;
168 unsigned long write_from;
169 unsigned long blocksize = inode->i_sb->s_blocksize;
171 if (inode->i_size == 0) {
172 REISERFS_I(inode)->i_flags |= i_nopack_mask;
175 /* ioctl already done */
176 if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
180 /* we need to make sure nobody is changing the file size beneath
183 mutex_lock(&inode->i_mutex);
184 reiserfs_write_lock(inode->i_sb);
186 write_from = inode->i_size & (blocksize - 1);
187 /* if we are on a block boundary, we are already unpacked. */
188 if (write_from == 0) {
189 REISERFS_I(inode)->i_flags |= i_nopack_mask;
193 /* we unpack by finding the page with the tail, and calling
194 ** reiserfs_prepare_write on that page. This will force a
195 ** reiserfs_get_block to unpack the tail for us.
197 index = inode->i_size >> PAGE_CACHE_SHIFT;
198 mapping = inode->i_mapping;
199 page = grab_cache_page(mapping, index);
204 retval = reiserfs_prepare_write(NULL, page, write_from, write_from);
208 /* conversion can change page contents, must flush */
209 flush_dcache_page(page);
210 retval = reiserfs_commit_write(NULL, page, write_from, write_from);
211 REISERFS_I(inode)->i_flags |= i_nopack_mask;
215 page_cache_release(page);
218 mutex_unlock(&inode->i_mutex);
219 reiserfs_write_unlock(inode->i_sb);