xref: /linux/fs/ubifs/ioctl.c (revision 367b8112fe2ea5c39a7bb4d263dcdd9b612fae18)
1 /*
2  * This file is part of UBIFS.
3  *
4  * Copyright (C) 2006-2008 Nokia Corporation.
5  * Copyright (C) 2006, 2007 University of Szeged, Hungary
6  *
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.
10  *
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
14  * more details.
15  *
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
19  *
20  * Authors: Zoltan Sogor
21  *          Artem Bityutskiy (Битюцкий Артём)
22  *          Adrian Hunter
23  */
24 
25 /* This file implements EXT2-compatible extended attribute ioctl() calls */
26 
27 #include <linux/compat.h>
28 #include <linux/smp_lock.h>
29 #include <linux/mount.h>
30 #include "ubifs.h"
31 
32 /**
33  * ubifs_set_inode_flags - set VFS inode flags.
34  * @inode: VFS inode to set flags for
35  *
36  * This function propagates flags from UBIFS inode object to VFS inode object.
37  */
38 void ubifs_set_inode_flags(struct inode *inode)
39 {
40 	unsigned int flags = ubifs_inode(inode)->flags;
41 
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;
51 }
52 
53 /*
54  * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
55  * @ioctl_flags: flags to convert
56  *
57  * This function convert ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
58  * (@UBIFS_COMPR_FL, etc).
59  */
60 static int ioctl2ubifs(int ioctl_flags)
61 {
62 	int ubifs_flags = 0;
63 
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;
74 
75 	return ubifs_flags;
76 }
77 
78 /*
79  * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
80  * @ubifs_flags: flags to convert
81  *
82  * This function convert UBIFS (@UBIFS_COMPR_FL, etc) to ioctl flags
83  * (@FS_COMPR_FL, etc).
84  */
85 static int ubifs2ioctl(int ubifs_flags)
86 {
87 	int ioctl_flags = 0;
88 
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;
99 
100 	return ioctl_flags;
101 }
102 
103 static int setflags(struct inode *inode, int flags)
104 {
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 };
110 
111 	err = ubifs_budget_space(c, &req);
112 	if (err)
113 		return err;
114 
115 	/*
116 	 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
117 	 * the relevant capability.
118 	 */
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)) {
123 			err = -EPERM;
124 			goto out_unlock;
125 		}
126 	}
127 
128 	ui->flags = ioctl2ubifs(flags);
129 	ubifs_set_inode_flags(inode);
130 	inode->i_ctime = ubifs_current_time(inode);
131 	release = ui->dirty;
132 	mark_inode_dirty_sync(inode);
133 	mutex_unlock(&ui->ui_mutex);
134 
135 	if (release)
136 		ubifs_release_budget(c, &req);
137 	if (IS_SYNC(inode))
138 		err = write_inode_now(inode, 1);
139 	return err;
140 
141 out_unlock:
142 	ubifs_err("can't modify inode %lu attributes", inode->i_ino);
143 	mutex_unlock(&ui->ui_mutex);
144 	ubifs_release_budget(c, &req);
145 	return err;
146 }
147 
148 long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
149 {
150 	int flags, err;
151 	struct inode *inode = file->f_path.dentry->d_inode;
152 
153 	switch (cmd) {
154 	case FS_IOC_GETFLAGS:
155 		flags = ubifs2ioctl(ubifs_inode(inode)->flags);
156 
157 		return put_user(flags, (int __user *) arg);
158 
159 	case FS_IOC_SETFLAGS: {
160 		if (IS_RDONLY(inode))
161 			return -EROFS;
162 
163 		if (!is_owner_or_cap(inode))
164 			return -EACCES;
165 
166 		if (get_user(flags, (int __user *) arg))
167 			return -EFAULT;
168 
169 		if (!S_ISDIR(inode->i_mode))
170 			flags &= ~FS_DIRSYNC_FL;
171 
172 		/*
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.
175 		 */
176 		err = mnt_want_write(file->f_path.mnt);
177 		if (err)
178 			return err;
179 		err = setflags(inode, flags);
180 		mnt_drop_write(file->f_path.mnt);
181 		return err;
182 	}
183 
184 	default:
185 		return -ENOTTY;
186 	}
187 }
188 
189 #ifdef CONFIG_COMPAT
190 long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
191 {
192 	switch (cmd) {
193 	case FS_IOC32_GETFLAGS:
194 		cmd = FS_IOC_GETFLAGS;
195 		break;
196 	case FS_IOC32_SETFLAGS:
197 		cmd = FS_IOC_SETFLAGS;
198 		break;
199 	default:
200 		return -ENOIOCTLCMD;
201 	}
202 	return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
203 }
204 #endif
205