xref: /openbmc/linux/fs/ocfs2/ioctl.c (revision f42b3800)
1 /*
2  * linux/fs/ocfs2/ioctl.c
3  *
4  * Copyright (C) 2006 Herbert Poetzl
5  * adapted from Remy Card's ext2/ioctl.c
6  */
7 
8 #include <linux/fs.h>
9 #include <linux/mount.h>
10 #include <linux/smp_lock.h>
11 
12 #define MLOG_MASK_PREFIX ML_INODE
13 #include <cluster/masklog.h>
14 
15 #include "ocfs2.h"
16 #include "alloc.h"
17 #include "dlmglue.h"
18 #include "file.h"
19 #include "inode.h"
20 #include "journal.h"
21 
22 #include "ocfs2_fs.h"
23 #include "ioctl.h"
24 #include "resize.h"
25 
26 #include <linux/ext2_fs.h>
27 
28 static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
29 {
30 	int status;
31 
32 	status = ocfs2_inode_lock(inode, NULL, 0);
33 	if (status < 0) {
34 		mlog_errno(status);
35 		return status;
36 	}
37 	ocfs2_get_inode_flags(OCFS2_I(inode));
38 	*flags = OCFS2_I(inode)->ip_attr;
39 	ocfs2_inode_unlock(inode, 0);
40 
41 	mlog_exit(status);
42 	return status;
43 }
44 
45 static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
46 				unsigned mask)
47 {
48 	struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
49 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
50 	handle_t *handle = NULL;
51 	struct buffer_head *bh = NULL;
52 	unsigned oldflags;
53 	int status;
54 
55 	mutex_lock(&inode->i_mutex);
56 
57 	status = ocfs2_inode_lock(inode, &bh, 1);
58 	if (status < 0) {
59 		mlog_errno(status);
60 		goto bail;
61 	}
62 
63 	status = -EROFS;
64 	if (IS_RDONLY(inode))
65 		goto bail_unlock;
66 
67 	status = -EACCES;
68 	if (!is_owner_or_cap(inode))
69 		goto bail_unlock;
70 
71 	if (!S_ISDIR(inode->i_mode))
72 		flags &= ~OCFS2_DIRSYNC_FL;
73 
74 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
75 	if (IS_ERR(handle)) {
76 		status = PTR_ERR(handle);
77 		mlog_errno(status);
78 		goto bail_unlock;
79 	}
80 
81 	oldflags = ocfs2_inode->ip_attr;
82 	flags = flags & mask;
83 	flags |= oldflags & ~mask;
84 
85 	/*
86 	 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
87 	 * the relevant capability.
88 	 */
89 	status = -EPERM;
90 	if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
91 		(OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
92 		if (!capable(CAP_LINUX_IMMUTABLE))
93 			goto bail_unlock;
94 	}
95 
96 	ocfs2_inode->ip_attr = flags;
97 	ocfs2_set_inode_flags(inode);
98 
99 	status = ocfs2_mark_inode_dirty(handle, inode, bh);
100 	if (status < 0)
101 		mlog_errno(status);
102 
103 	ocfs2_commit_trans(osb, handle);
104 bail_unlock:
105 	ocfs2_inode_unlock(inode, 1);
106 bail:
107 	mutex_unlock(&inode->i_mutex);
108 
109 	if (bh)
110 		brelse(bh);
111 
112 	mlog_exit(status);
113 	return status;
114 }
115 
116 long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
117 {
118 	struct inode *inode = filp->f_path.dentry->d_inode;
119 	unsigned int flags;
120 	int new_clusters;
121 	int status;
122 	struct ocfs2_space_resv sr;
123 	struct ocfs2_new_group_input input;
124 
125 	switch (cmd) {
126 	case OCFS2_IOC_GETFLAGS:
127 		status = ocfs2_get_inode_attr(inode, &flags);
128 		if (status < 0)
129 			return status;
130 
131 		flags &= OCFS2_FL_VISIBLE;
132 		return put_user(flags, (int __user *) arg);
133 	case OCFS2_IOC_SETFLAGS:
134 		if (get_user(flags, (int __user *) arg))
135 			return -EFAULT;
136 
137 		return ocfs2_set_inode_attr(inode, flags,
138 			OCFS2_FL_MODIFIABLE);
139 	case OCFS2_IOC_RESVSP:
140 	case OCFS2_IOC_RESVSP64:
141 	case OCFS2_IOC_UNRESVSP:
142 	case OCFS2_IOC_UNRESVSP64:
143 		if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
144 			return -EFAULT;
145 
146 		return ocfs2_change_file_space(filp, cmd, &sr);
147 	case OCFS2_IOC_GROUP_EXTEND:
148 		if (!capable(CAP_SYS_RESOURCE))
149 			return -EPERM;
150 
151 		if (get_user(new_clusters, (int __user *)arg))
152 			return -EFAULT;
153 
154 		return ocfs2_group_extend(inode, new_clusters);
155 	case OCFS2_IOC_GROUP_ADD:
156 	case OCFS2_IOC_GROUP_ADD64:
157 		if (!capable(CAP_SYS_RESOURCE))
158 			return -EPERM;
159 
160 		if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
161 			return -EFAULT;
162 
163 		return ocfs2_group_add(inode, &input);
164 	default:
165 		return -ENOTTY;
166 	}
167 }
168 
169 #ifdef CONFIG_COMPAT
170 long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
171 {
172 	switch (cmd) {
173 	case OCFS2_IOC32_GETFLAGS:
174 		cmd = OCFS2_IOC_GETFLAGS;
175 		break;
176 	case OCFS2_IOC32_SETFLAGS:
177 		cmd = OCFS2_IOC_SETFLAGS;
178 		break;
179 	case OCFS2_IOC_RESVSP:
180 	case OCFS2_IOC_RESVSP64:
181 	case OCFS2_IOC_UNRESVSP:
182 	case OCFS2_IOC_UNRESVSP64:
183 	case OCFS2_IOC_GROUP_EXTEND:
184 	case OCFS2_IOC_GROUP_ADD:
185 	case OCFS2_IOC_GROUP_ADD64:
186 		break;
187 	default:
188 		return -ENOIOCTLCMD;
189 	}
190 
191 	return ocfs2_ioctl(file, cmd, arg);
192 }
193 #endif
194