1/*
2 * linux/fs/ext2/ioctl.c
3 *
4 * Copyright (C) 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 */
9
10#include <linux/fs.h>
11#include <linux/ext2_fs.h>
12#include <linux/sched.h>
13#include <asm/uaccess.h>
14
15
16int ext2_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
17		unsigned long arg)
18{
19	unsigned int flags;
20
21	ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
22
23	switch (cmd) {
24	case EXT2_IOC_GETFLAGS:
25		flags = inode->u.ext2_i.i_flags & EXT2_FL_USER_VISIBLE;
26		return put_user(flags, (int *) arg);
27	case EXT2_IOC_SETFLAGS: {
28		unsigned int oldflags;
29
30		if (IS_RDONLY(inode))
31			return -EROFS;
32
33		if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
34			return -EACCES;
35
36		if (get_user(flags, (int *) arg))
37			return -EFAULT;
38
39		oldflags = inode->u.ext2_i.i_flags;
40
41		/*
42		 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
43		 * the relevant capability.
44		 *
45		 * This test looks nicer. Thanks to Pauline Middelink
46		 */
47		if ((flags ^ oldflags) & (EXT2_APPEND_FL | EXT2_IMMUTABLE_FL)) {
48			if (!capable(CAP_LINUX_IMMUTABLE))
49				return -EPERM;
50		}
51
52		flags = flags & EXT2_FL_USER_MODIFIABLE;
53		flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
54		inode->u.ext2_i.i_flags = flags;
55
56		if (flags & EXT2_SYNC_FL)
57			inode->i_flags |= S_SYNC;
58		else
59			inode->i_flags &= ~S_SYNC;
60		if (flags & EXT2_APPEND_FL)
61			inode->i_flags |= S_APPEND;
62		else
63			inode->i_flags &= ~S_APPEND;
64		if (flags & EXT2_IMMUTABLE_FL)
65			inode->i_flags |= S_IMMUTABLE;
66		else
67			inode->i_flags &= ~S_IMMUTABLE;
68		if (flags & EXT2_NOATIME_FL)
69			inode->i_flags |= S_NOATIME;
70		else
71			inode->i_flags &= ~S_NOATIME;
72		inode->i_ctime = CURRENT_TIME;
73		mark_inode_dirty(inode);
74		return 0;
75	}
76	case EXT2_IOC_GETVERSION:
77		return put_user(inode->i_generation, (int *) arg);
78	case EXT2_IOC_SETVERSION:
79		if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
80			return -EPERM;
81		if (IS_RDONLY(inode))
82			return -EROFS;
83		if (get_user(inode->i_generation, (int *) arg))
84			return -EFAULT;
85		inode->i_ctime = CURRENT_TIME;
86		mark_inode_dirty(inode);
87		return 0;
88	default:
89		return -ENOTTY;
90	}
91}
92