2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
5 #include <linux/capability.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 #include <linux/compat.h>
14 static int reiserfs_unpack(struct inode *inode, struct file *filp);
17 ** reiserfs_ioctl - handler for ioctl for inode
18 ** supported commands:
19 ** 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
20 ** and prevent packing file (argument arg has to be non-zero)
21 ** 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
22 ** 3) That's all for a while ...
24 int reiserfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
30 case REISERFS_IOC_UNPACK:
31 if (S_ISREG(inode->i_mode)) {
33 return reiserfs_unpack(inode, filp);
38 /* following two cases are taken from fs/ext2/ioctl.c by Remy
39 Card (card@masi.ibp.fr) */
40 case REISERFS_IOC_GETFLAGS:
41 if (!reiserfs_attrs(inode->i_sb))
44 flags = REISERFS_I(inode)->i_attrs;
45 i_attrs_to_sd_attrs(inode, (__u16 *) & flags);
46 return put_user(flags, (int __user *)arg);
47 case REISERFS_IOC_SETFLAGS:{
48 if (!reiserfs_attrs(inode->i_sb))
54 if ((current->fsuid != inode->i_uid)
55 && !capable(CAP_FOWNER))
58 if (get_user(flags, (int __user *)arg))
61 if (((flags ^ REISERFS_I(inode)->
62 i_attrs) & (REISERFS_IMMUTABLE_FL |
64 && !capable(CAP_LINUX_IMMUTABLE))
67 if ((flags & REISERFS_NOTAIL_FL) &&
68 S_ISREG(inode->i_mode)) {
71 result = reiserfs_unpack(inode, filp);
75 sd_attrs_to_i_attrs(flags, inode);
76 REISERFS_I(inode)->i_attrs = flags;
77 inode->i_ctime = CURRENT_TIME_SEC;
78 mark_inode_dirty(inode);
81 case REISERFS_IOC_GETVERSION:
82 return put_user(inode->i_generation, (int __user *)arg);
83 case REISERFS_IOC_SETVERSION:
84 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
88 if (get_user(inode->i_generation, (int __user *)arg))
90 inode->i_ctime = CURRENT_TIME_SEC;
91 mark_inode_dirty(inode);
99 long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
102 struct inode *inode = file->f_path.dentry->d_inode;
105 /* These are just misnamed, they actually get/put from/to user an int */
107 case REISERFS_IOC32_UNPACK:
108 cmd = REISERFS_IOC_UNPACK;
110 case REISERFS_IOC32_GETFLAGS:
111 cmd = REISERFS_IOC_GETFLAGS;
113 case REISERFS_IOC32_SETFLAGS:
114 cmd = REISERFS_IOC_SETFLAGS;
116 case REISERFS_IOC32_GETVERSION:
117 cmd = REISERFS_IOC_GETVERSION;
119 case REISERFS_IOC32_SETVERSION:
120 cmd = REISERFS_IOC_SETVERSION;
126 ret = reiserfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
134 ** Function try to convert tail from direct item into indirect.
135 ** It set up nopack attribute in the REISERFS_I(inode)->nopack
137 static int reiserfs_unpack(struct inode *inode, struct file *filp)
142 struct address_space *mapping;
143 unsigned long write_from;
144 unsigned long blocksize = inode->i_sb->s_blocksize;
146 if (inode->i_size == 0) {
147 REISERFS_I(inode)->i_flags |= i_nopack_mask;
150 /* ioctl already done */
151 if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
155 /* we need to make sure nobody is changing the file size beneath
158 mutex_lock(&inode->i_mutex);
159 reiserfs_write_lock(inode->i_sb);
161 write_from = inode->i_size & (blocksize - 1);
162 /* if we are on a block boundary, we are already unpacked. */
163 if (write_from == 0) {
164 REISERFS_I(inode)->i_flags |= i_nopack_mask;
168 /* we unpack by finding the page with the tail, and calling
169 ** reiserfs_prepare_write on that page. This will force a
170 ** reiserfs_get_block to unpack the tail for us.
172 index = inode->i_size >> PAGE_CACHE_SHIFT;
173 mapping = inode->i_mapping;
174 page = grab_cache_page(mapping, index);
180 mapping->a_ops->prepare_write(NULL, page, write_from, write_from);
184 /* conversion can change page contents, must flush */
185 flush_dcache_page(page);
187 mapping->a_ops->commit_write(NULL, page, write_from, write_from);
188 REISERFS_I(inode)->i_flags |= i_nopack_mask;
192 page_cache_release(page);
195 mutex_unlock(&inode->i_mutex);
196 reiserfs_write_unlock(inode->i_sb);