2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 * Copyright (C) 2006, 2007 University of Szeged, Hungary
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Authors: Zoltan Sogor
21 * Artem Bityutskiy (Битюцкий Артём)
25 /* This file implements EXT2-compatible extended attribute ioctl() calls */
27 #include <linux/compat.h>
28 #include <linux/smp_lock.h>
29 #include <linux/mount.h>
33 * ubifs_set_inode_flags - set VFS inode flags.
34 * @inode: VFS inode to set flags for
36 * This function propagates flags from UBIFS inode object to VFS inode object.
38 void ubifs_set_inode_flags(struct inode *inode)
40 unsigned int flags = ubifs_inode(inode)->flags;
42 inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC);
43 if (flags & UBIFS_SYNC_FL)
44 inode->i_flags |= S_SYNC;
45 if (flags & UBIFS_APPEND_FL)
46 inode->i_flags |= S_APPEND;
47 if (flags & UBIFS_IMMUTABLE_FL)
48 inode->i_flags |= S_IMMUTABLE;
49 if (flags & UBIFS_DIRSYNC_FL)
50 inode->i_flags |= S_DIRSYNC;
54 * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
55 * @ioctl_flags: flags to convert
57 * This function convert ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
58 * (@UBIFS_COMPR_FL, etc).
60 static int ioctl2ubifs(int ioctl_flags)
64 if (ioctl_flags & FS_COMPR_FL)
65 ubifs_flags |= UBIFS_COMPR_FL;
66 if (ioctl_flags & FS_SYNC_FL)
67 ubifs_flags |= UBIFS_SYNC_FL;
68 if (ioctl_flags & FS_APPEND_FL)
69 ubifs_flags |= UBIFS_APPEND_FL;
70 if (ioctl_flags & FS_IMMUTABLE_FL)
71 ubifs_flags |= UBIFS_IMMUTABLE_FL;
72 if (ioctl_flags & FS_DIRSYNC_FL)
73 ubifs_flags |= UBIFS_DIRSYNC_FL;
79 * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
80 * @ubifs_flags: flags to convert
82 * This function convert UBIFS (@UBIFS_COMPR_FL, etc) to ioctl flags
83 * (@FS_COMPR_FL, etc).
85 static int ubifs2ioctl(int ubifs_flags)
89 if (ubifs_flags & UBIFS_COMPR_FL)
90 ioctl_flags |= FS_COMPR_FL;
91 if (ubifs_flags & UBIFS_SYNC_FL)
92 ioctl_flags |= FS_SYNC_FL;
93 if (ubifs_flags & UBIFS_APPEND_FL)
94 ioctl_flags |= FS_APPEND_FL;
95 if (ubifs_flags & UBIFS_IMMUTABLE_FL)
96 ioctl_flags |= FS_IMMUTABLE_FL;
97 if (ubifs_flags & UBIFS_DIRSYNC_FL)
98 ioctl_flags |= FS_DIRSYNC_FL;
103 static int setflags(struct inode *inode, int flags)
105 int oldflags, err, release;
106 struct ubifs_inode *ui = ubifs_inode(inode);
107 struct ubifs_info *c = inode->i_sb->s_fs_info;
108 struct ubifs_budget_req req = { .dirtied_ino = 1,
109 .dirtied_ino_d = ui->data_len };
111 err = ubifs_budget_space(c, &req);
116 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
117 * the relevant capability.
119 mutex_lock(&ui->ui_mutex);
120 oldflags = ubifs2ioctl(ui->flags);
121 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
122 if (!capable(CAP_LINUX_IMMUTABLE)) {
128 ui->flags = ioctl2ubifs(flags);
129 ubifs_set_inode_flags(inode);
130 inode->i_ctime = ubifs_current_time(inode);
132 mark_inode_dirty_sync(inode);
133 mutex_unlock(&ui->ui_mutex);
136 ubifs_release_budget(c, &req);
138 err = write_inode_now(inode, 1);
142 ubifs_err("can't modify inode %lu attributes", inode->i_ino);
143 mutex_unlock(&ui->ui_mutex);
144 ubifs_release_budget(c, &req);
148 long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
151 struct inode *inode = file->f_path.dentry->d_inode;
154 case FS_IOC_GETFLAGS:
155 flags = ubifs2ioctl(ubifs_inode(inode)->flags);
157 return put_user(flags, (int __user *) arg);
159 case FS_IOC_SETFLAGS: {
160 if (IS_RDONLY(inode))
163 if (!is_owner_or_cap(inode))
166 if (get_user(flags, (int __user *) arg))
169 if (!S_ISDIR(inode->i_mode))
170 flags &= ~FS_DIRSYNC_FL;
173 * Make sure the file-system is read-write and make sure it
174 * will not become read-only while we are changing the flags.
176 err = mnt_want_write(file->f_path.mnt);
179 err = setflags(inode, flags);
180 mnt_drop_write(file->f_path.mnt);
190 long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
193 case FS_IOC32_GETFLAGS:
194 cmd = FS_IOC_GETFLAGS;
196 case FS_IOC32_SETFLAGS:
197 cmd = FS_IOC_SETFLAGS;
202 return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));