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,
28 unsigned int flags, oldflags;
31 case REISERFS_IOC_UNPACK:
32 if (S_ISREG(inode->i_mode)) {
34 return reiserfs_unpack(inode, filp);
39 /* following two cases are taken from fs/ext2/ioctl.c by Remy
40 Card (card@masi.ibp.fr) */
41 case REISERFS_IOC_GETFLAGS:
42 if (!reiserfs_attrs(inode->i_sb))
45 flags = REISERFS_I(inode)->i_attrs;
46 i_attrs_to_sd_attrs(inode, (__u16 *) & flags);
47 flags &= REISERFS_FL_USER_VISIBLE;
48 return put_user(flags, (int __user *)arg);
49 case REISERFS_IOC_SETFLAGS:{
50 if (!reiserfs_attrs(inode->i_sb))
53 if (IS_RDONLY(inode) ||
54 (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
57 if ((current->fsuid != inode->i_uid)
58 && !capable(CAP_FOWNER))
61 if (get_user(flags, (int __user *)arg))
64 oldflags = REISERFS_I(inode) -> i_attrs;
65 if (((oldflags & REISERFS_IMMUTABLE_FL) ||
67 (REISERFS_IMMUTABLE_FL | REISERFS_IUNLINK_FL |
68 REISERFS_APPEND_FL))) &&
69 !capable(CAP_LINUX_IMMUTABLE))
72 if ((flags & REISERFS_NOTAIL_FL) &&
73 S_ISREG(inode->i_mode)) {
76 result = reiserfs_unpack(inode, filp);
81 flags = flags & REISERFS_FL_USER_MODIFIABLE;
82 flags |= oldflags & ~REISERFS_FL_USER_MODIFIABLE;
83 sd_attrs_to_i_attrs(flags, inode);
84 REISERFS_I(inode)->i_attrs = flags;
85 inode->i_ctime = CURRENT_TIME_SEC;
86 mark_inode_dirty(inode);
89 case REISERFS_IOC_GETVERSION:
90 return put_user(inode->i_generation, (int __user *)arg);
91 case REISERFS_IOC_SETVERSION:
92 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
94 if (IS_RDONLY(inode) ||
95 (filp && MNT_IS_RDONLY(filp->f_vfsmnt)))
97 if (get_user(inode->i_generation, (int __user *)arg))
99 inode->i_ctime = CURRENT_TIME_SEC;
100 mark_inode_dirty(inode);
108 long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
111 struct inode *inode = file->f_path.dentry->d_inode;
114 /* These are just misnamed, they actually get/put from/to user an int */
116 case REISERFS_IOC32_UNPACK:
117 cmd = REISERFS_IOC_UNPACK;
119 case REISERFS_IOC32_GETFLAGS:
120 cmd = REISERFS_IOC_GETFLAGS;
122 case REISERFS_IOC32_SETFLAGS:
123 cmd = REISERFS_IOC_SETFLAGS;
125 case REISERFS_IOC32_GETVERSION:
126 cmd = REISERFS_IOC_GETVERSION;
128 case REISERFS_IOC32_SETVERSION:
129 cmd = REISERFS_IOC_SETVERSION;
135 ret = reiserfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
143 ** Function try to convert tail from direct item into indirect.
144 ** It set up nopack attribute in the REISERFS_I(inode)->nopack
146 static int reiserfs_unpack(struct inode *inode, struct file *filp)
151 struct address_space *mapping;
152 unsigned long write_from;
153 unsigned long blocksize = inode->i_sb->s_blocksize;
155 if (inode->i_size == 0) {
156 REISERFS_I(inode)->i_flags |= i_nopack_mask;
159 /* ioctl already done */
160 if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
164 /* we need to make sure nobody is changing the file size beneath
167 mutex_lock(&inode->i_mutex);
168 reiserfs_write_lock(inode->i_sb);
170 write_from = inode->i_size & (blocksize - 1);
171 /* if we are on a block boundary, we are already unpacked. */
172 if (write_from == 0) {
173 REISERFS_I(inode)->i_flags |= i_nopack_mask;
177 /* we unpack by finding the page with the tail, and calling
178 ** reiserfs_prepare_write on that page. This will force a
179 ** reiserfs_get_block to unpack the tail for us.
181 index = inode->i_size >> PAGE_CACHE_SHIFT;
182 mapping = inode->i_mapping;
183 page = grab_cache_page(mapping, index);
189 mapping->a_ops->prepare_write(NULL, page, write_from, write_from);
193 /* conversion can change page contents, must flush */
194 flush_dcache_page(page);
196 mapping->a_ops->commit_write(NULL, page, write_from, write_from);
197 REISERFS_I(inode)->i_flags |= i_nopack_mask;
201 page_cache_release(page);
204 mutex_unlock(&inode->i_mutex);
205 reiserfs_write_unlock(inode->i_sb);